Python asks for POST error 400
I have searched many questions similar to mine, but I could not find a solution.
I am using requests to make a POST request. I've tried many combinations inside the request, but nothing returns 201 ok.
Here is my code:
import json
import requests
if __name__ == '__main__':
headers = {'content-type' : 'application/json'}
url = "http://myserver/ext/v3.1/test_device"
message = {"atribute_a": "value", "atribute_b": "valueb"}
params = {"priority":"normal"}
r = requests.post(url, params=params, headers=headers, data = json.dumps(message) )
print(r)
I also tried using json.dumps but it also gives me 400 bad requests. I've also tried adding parameters directly to the url, like: ...? Priority = normal, but no success.
+3
source to share
1 answer
Based on the comments, your server is actually expecting the data to be a compressed JSON object. As far as parameters are concerned, it will most likely help if they are declared as tuples of tuples (or dict of dicts)
Try the following:
headers = {
'content-type': 'application/json',
}
params = (
('priority', 'normal'),
)
data = {
"atribute_a": "value",
"atribute_b": false
}
requests.post(url, headers=headers, params=params, data=str(data))
+4
source to share