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.

3
  • 3
    The 400 error is generated by the server; requests did its job just fine. There is nothing we can help you with here. Commented Nov 8, 2013 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? Commented Nov 8, 2013 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 ? Commented Nov 12, 2013 at 8:18

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

*json.dumps(params)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.