简体   繁体   中英

Changing query parameters throws 405 ERROR in REST GET API Call

I am trying to simulate via Python the REST API calls this website makes while calling for a typical security (try these selections: Get Historical Data for: "Security-wise Price Volume and Deliverable Positions of Data", Enter Symbol: "LaurusLabs", Select Series: "ALL", Select a Time Period: 22-01-2021 to 10-02-2021)

Here is my code and the result of tinkering:

```python
import requests
from pprint import PrettyPrinter as pp
import urllib
printer = pp(indent=0)
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
    "Dnt": "1",
    "Connection": "keep-alive",
    "Host": "www1.nseindia.com",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"}

session = requests.Session()
url_symb_count = "https://www1.nseindia.com/marketinfo/sym_map/symbolCount.jsp?symbol=LAURUSLABS"
response = requests.get(url_symb_count, headers = headers)
cookies = dict(response.cookies)
x = (response.text).strip()
#printer.pprint(cookies)

f = { "symbol": "LAURUSLABS","segmentLink": "3","symbolCount": 1,"series": "ALL","dateRange": " ","fromDate": "20-01-2021","toDate": "10-02-2021","dataType": "PRICEVOLUMEDELIVERABLE"}
url = 'https://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?' + urllib.parse.urlencode(f)
response = session.get(url, headers = headers, cookies=cookies)
x = (response.text)
printer.pprint(x)
printer.pprint(url)

```

The problem is whenever I try to query for a different date range, the result appears to be giving a Status 405 error. This link in fact seems to be working only for this selection, but it beats me how or why?

Can you please help me decipher why exactly a straight manipulation of the dates is giving me a different result?

So i've been playing around with this for half an hour and this seems to be working consistently, whenever I change from and to date in my f dictionary.


import requests
from pprint import PrettyPrinter as pp
import urllib

printer = pp(indent=2)
headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.5",
    "Connection": "keep-alive",
    "Dnt": "1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
    "Host": "www1.nseindia.com",
    "Referer": "https://www1.nseindia.com/products/content/equities/equities/eq_security.htm",
    "Sec-GPC": "1",
    "X-Requested-With": "XMLHttpRequest"
}

f = {
    "symbol": "LAURUSLABS",
    "segmentLink": "3",
    "symbolCount": 1,
    "series": "ALL",
    "dateRange": " ",
    "fromDate": "04-05-2020",
    "toDate": "02-05-2021",
    "dataType": "PRICEVOLUMEDELIVERABLE"
}
url = 'https://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?' + urllib.parse.urlencode(f)
response = requests.get(url, headers=headers)
x = (response.text)
printer.pprint(x)

   

Edit for explanation:

I was just observing the networking tab while browsing the link OP had provided, and noticed all requests originate from "eq_security.htm", so I just copied as many headers as I could to match. I thought maybe it was the request type, or the date formatting, but neither was the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM