bad request error 400 while using python requests.post function

Toussah picture Toussah · Nov 8, 2013 · Viewed 11.3k times · Source

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.

Answer

Aldo Escobar picture Aldo Escobar · Apr 25, 2016

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)