Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python POST request and null value

I'm using the following code , to do POST request on application level

url = 'http://www.webdev.com/web/POST'
headers = {'content-type': 'application/json'}
payload = {'name': 'name', 'status': 'success', 'newname': 'name'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
r.text
r.status_code
r.connection.close()

from Python logs

send: 'POST /web/POST HTTP/1.1\r\nHost: www.webdev.com\r\nContent-Length: 189\r\nUser-Agent: python-requests/2.8.1\r\nConnection: keep-alive\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\n\r\n'
send: '{"name": "test1", "status": "sucsses", "newname": "name23"}'
reply: 'HTTP/1.1 200 OK\r\n'

but on the application level, it gives null value for the 3 vars.

The application is expecting the following format:

http://www.webdev.com/web/POST?name=test1&status=sucsses&newname=name23

I used curl command to do the post and application got the right value

like image 249
Jecki Avatar asked Sep 25 '26 22:09

Jecki


1 Answers

You aren't passing your params as part of url and sending them in POST body, but server is waiting for GET params.

To pass your payload as GET parameters use params keyword argument:

r = requests.post(url, params=payload, headers=headers)

According to your url format you should ignore passing data, simply pass params.

Good luck!

like image 173
Andriy Ivaneyko Avatar answered Sep 28 '26 12:09

Andriy Ivaneyko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!