5

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.

Dhia
  • 10,119
  • 11
  • 58
  • 69
Recoba20
  • 314
  • 1
  • 13
  • The data structures are different between your examples. Also note you can pass URL query parameters differently in `requests`, rather than in the URL string: http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls. – jonrsharpe Dec 21 '17 at 14:11
  • yeah, i know about the data structure, but requests fails if i put them like the exact one in curl. thanks for the query parameters, but i assume even if i pass them, it's still gonna behave like this. – Recoba20 Dec 21 '17 at 14:14
  • What do you mean by *"fails"*? The current one also fails, so I assume it's different somehow? Give a [mcve]. – jonrsharpe Dec 21 '17 at 14:15
  • Did you try passing your data as JSON? – cs95 Dec 21 '17 at 14:15

1 Answers1

6

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"}])
Dhia
  • 10,119
  • 11
  • 58
  • 69