Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Requests HTTP Response 406

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'}
like image 394
DNac Avatar asked May 12 '19 17:05

DNac


Video Answer


1 Answers

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

like image 78
DNac Avatar answered Sep 19 '22 20:09

DNac