https Get request failing on first try - python request

Viewed 413

While executing a GET request on api.nasdaq.com, I am getting error response 400 - The request has an invalid header name. But If I hit the same URL in a browser, it does return a valid response.

Also AFTER hitting the url via browser, below code then works fine. If I use a new value for symbol variable then again it fails with 400 . Each time once I try the url in browser and then run this code it works but not first time.

Is this behavior due to any cookie usage? Any help is appreciated.

import requests

symbol = 'ALB'
url = "https://api.nasdaq.com/api/quote/{}/historical?assetclass=stocks&fromdate=2021-07-06&todate=2021-07-07".format(symbol)
headers = {
                "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                "Accept-Encoding":"gzip, deflate, br",
                "Accept-Language":"en-US,en;q=0.9",
                "Connection":"keep-alive",
                "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36",
                "cache - control":"max-age=0, no-cache, no-store",
                "upgrade-insecure-requests": "1"
    }
response = requests.get(url, headers = headers, verify=True)
if response.status_code != 200:
    print("download response code:{}".format(response.status_code))
    print("download response text:{}".format(response.text))
else:
    print(response.text)

Failure response:

download response code:400
download response text:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>

various symbols that can used to test this is AMZN, AAPL, MSFT, ALB, ABC, SQ

1 Answers

Using these headers get me a 200 response. I capitalized the cache control and upgrade insecure request.

I can't help you with the cookie usage question unfortunately.

headers = {
                "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                "Accept-Encoding":"gzip, deflate, br",
                "Accept-Language":"en-US,en;q=0.9",
                "Connection":"keep-alive",
                "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36",
                "Cache-Control":"max-age=0, no-cache, no-store",
                "Upgrade-Insecure-Requests": "1"
    }
Related