3

I'm trying to make a simple post request via the requests library of Python and I get a bad request error (400) while my url is supposedly correct since I can use it to perform a get. I'm very new in REST requests, I read many tutorials and documentation but I guess there are still things I don't get so my error could be basic. Maybe a lack of understanding on the type of url I'm supposed to send via POST. Here my code :

import requests
v_username = "username"
v_password = "password"
v_headers = {'content-type':'application/rdf+xml'}
url = 'https://my.url'
params = {'param': 'val_param'}
payload = {'data': 'my_data'}
r = requests.post(url, params = params, auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)
print r

I used the example of the requests documentation.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Toussah
  • 245
  • 1
  • 3
  • 14
  • 3
    The 400 error is generated by the server; `requests` did its job just fine. There is nothing we can help you with here. – Martijn Pieters Nov 08 '13 at 15:51
  • 1
    That Content-Type header is nonsense. When you pass a dictionary to the `data` argument, Requests builds and sends that data as `Content-Type: application/x-www-form-urlencoded`. Why are you setting it? – Lukasa Nov 08 '13 at 18:09
  • Well, it's the header I used to use when making Get requests so I thought it would work the same for a Put. I don't understand what I should do then, merge my data and my header in the same dictionary ? – Toussah Nov 12 '13 at 08:18

1 Answers1

3

I had a similar problem, i tried changing params to data or with json.dumps():

from json import dumps

r = requests.post(url, params=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)

or

r = requests.post(url, data=dumps(params), auth=(v_username, v_password), data=payload, headers=v_headers, verify=False)