简体   繁体   中英

Put request working in curl, but not in Python

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.

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"}])

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