13
votes

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'}
2
Sounds like you might need to add an accept header to the request. - Mark
@MarkMeyer do you have any example, please? I don't see anything like that in the doc. 2.python-requests.org//en/latest/api - DNac

2 Answers

27
votes

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

0
votes

Your own accepted answer is most likely incorrect. This error means the value of Accept header does not match Content-Type of the response. If you'd send Accept: */* , it would work (at least the 406 would not be returned).