I have my own domain where an json file is stored (http://example.com/file.json)
. When accesing the file in browser using the direct link, the json is returned just fine. But when using the same approach in my python code below, the http response is 406. Any ideas why?
import requests
url = 'http://example.com/file.json'
r = requests.get(url, headers={"Accept":"text/html"})
print(r.status_code)
print(r.headers)
Prints:
406
{'Server': 'nginx/1.14.1', 'Date': 'Sun, 12 May 2019 16:53:25 GMT', 'Content-Type': 'text/html; charset=iso-8859-1', 'Content-Length': '226', 'Connection': 'k
eep-alive'}
Solved by using a different User-Agent. The default Python User-Agent 'python-requests/2.21.0'
was being probably blocked by the hosting company.
r = requests.get(url, headers={"User-Agent": "XY"})
Some of the possible agents: List of User Agent strings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With