Skip to content

Fix broken Nifty Indices fetchers (index_history, index_pe_pb_div, index_total_returns) after API migration - #79

Open
0xmohitsen wants to merge 1 commit into
aeron7:masterfrom
0xmohitsen:master
Open

Fix broken Nifty Indices fetchers (index_history, index_pe_pb_div, index_total_returns) after API migration#79
0xmohitsen wants to merge 1 commit into
aeron7:masterfrom
0xmohitsen:master

Conversation

@0xmohitsen

Copy link
Copy Markdown

Problem

index_history, index_pe_pb_div, and index_total_returns fail with JSONDecodeError / KeyError: 'd' because niftyindices.com changed its API.

Old (broken):

  • URL: https://niftyindices.com/Backpage.aspx/...
  • Response: {"d": "[...]"}
  • No session cookies

New (working):

  • URL: https://www.niftyindices.com/BackPage/...
  • Response: direct JSON array
  • Session warmup required for ASP.NET_SessionId / Akamai cookies

Fix

  • Update niftyindices_headers (Chrome 150, www.niftyindices.com)
  • Add session warmup before POST
  • Parse both old {"d": "..."} and new direct-array responses
  • Raise a clear error when HTML is returned instead of JSON

Affected functions

  • index_history
  • index_pe_pb_div
  • index_total_returns

Tested

from nsepython import index_total_returns
df = index_total_returns("NIFTY 500", "26-Jul-2026", "29-Jul-2026")
print(df.head())  # returns valid DataFrame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant