Put request working in curl, but not in Python

2019-07-06 20:55发布

I am trying to make a 'put' method with curl everything is working fine and I got the JSON back:

curl -X PUT -d '[{"foo":"more_foo"}]' http://ip:6001/whatever?api_key=whatever

But for some reason when using the python requests module as follow:

import requests
url = 'http://ip:6001/whatever?api_key=whatever'

a = requests.put(url, data={"foo":"more_foo"})

print(a.text)
print(a.status_code)

I get the following error:

500 Internal Server Error

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

NB: The server is up and running.

1条回答
在下西门庆
2楼-- · 2019-07-06 21:35

The data should be dumped:

a = requests.put(url, data=json.dumps([{"foo":"more_foo"}]))

or you can use the json key instead of data:

a = requests.post(url, json=[{"foo":"more_foo"}])
查看更多
登录 后发表回答