Search code examples
pythonhttptornado

Simple not-authorized request to a Github API using Tornado httpclient returns Forbidden


I am trying to make a HTTP Request to a JSON API like https://api.github.com using tornado.httpclient and I found that it always responses with FORBIDDEN 403.

Simplifying, I make a request using the CLI with:

$ python -m tornado.httpclient https://api.github.com

getting a tornado.httpclient.HTTPError: HTTP 403: Forbidden.

In other hand, if I try to request this URL via browser or a simple $ curl https://api.github.com, the response is 200 OK and the proper JSON file.

What is causing this? Should I set some specific Headers on the tornado.httpclient request? What's the difference with a curl request?


Solution

  • You have to put user agent in the request, see Github API for more details:

    All API requests MUST include a valid User-Agent header. Requests with no User-Agent header will be rejected. We request that you use your GitHub username, or the name of your application, for the User-Agent header value. This allows us to contact you if there are problems