简体   繁体   中英

Curl works but python requests doesn't

When I do curl, I get a response:

root@3d7044bac92f:/home/app/tmp# curl -H "Content-type: application/json" -X GET https://github.com/timeline.json -k 

{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}

However, when I do python requests to the same URL I get a status 410.

import requests

headers = {
    'Content-type': 'application/json',
}

r = requests.get('https://github.com/timeline.json')
print r.json

root@3d7044bac92f:/home/app/tmp# python rest.py 
<bound method Response.json of <Response [410]>>

What gives?

The host is a standard Ubuntu docker image and only installed Curl and some python modules. Python -V is 2.7

Note: I looked at this question but I can't telnet into above server so that solution doesn't apply to me: Curl works but not Python requests

You've made at least two errors in your program.

1) You haven't specified the data= or headers parameters to the requests.get() call. Try this:

 r = requests.get('https://github.com/timeline.json', data=data, headers=headers)

2) .json is a method, not a data attribute of the response object. As a method, it must be called in order to be effective. Try this:

print r.json()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM