简体   繁体   中英

Implementing API into Python program

I am using Requests to communicate with a simple api.

The code looks like this:

payload = {'text': reply, 'name': varname, 'avatar': varavatar}
r = requests.get('http://example.de/create.json', params=payload)

However, the result looks like this:

{"result": "error", "error": "error_no_direct_connection"}

This might be a problem with the api but I'm not sure. So I printed out the URL that Requests created ( print r.url ). It looks like this: http://url.com/create.json?text=Test&name=myname&avatar=http%3A%2F%2Fa0.url.com%2Fnormal.png

To make sure it's not a problem oft the api I tried it with curl. The command works!

curl --data-urlencode "text=Test" -d "name=myname" -d "avatar=http://url.com/normal.png" http://url.com/create.json

The curl option --data causes curl to submit a POST request, not a GET request. Try request.post(..., data=...) .

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