Recoba20
Recoba20

Reputation: 324

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.

Upvotes: 5

Views: 1813

Answers (1)

Dhia
Dhia

Reputation: 10609

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

Upvotes: 6

Related Questions