This is the raw request for an API call:
POST http://192.168.3.45:8080/api/v2/event/log?sessionKey=b299d17b896417a7b18f46544d40adb734240cc2&format=json HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 86
Host: 192.168.3.45:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
{"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}"""
This request returns a success (2xx) response.
Now I am trying to post this request using requests
:
>>> import requests
>>> headers = {'content-type' : 'application/json'}
>>> data ={"eventType":"AAS_PORTAL_START","data{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}
>>> url = "http://192.168.3.45:8080/api/v2/event/log?sessionKey=9ebbd0b25760557393a43064a92bae539d962103&format=xml&platformId=1"
>>> requests.post(url,params=data,headers=headers)
<Response [400]>
Everything looks fine to me and I am not quite sure what I posting wrong to get a 400 response.
Set data to this:
params
is for GET-style URL parameters,data
is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.Your raw post contains JSON data though.
requests
can handle JSON encoding for you, and it'll set the correctContent-Header
too; all you need to do is pass in the Python object to be encoded as JSON into thejson
keyword argument.You could split out the URL parameters as well:
then post your data with:
The
json
keyword is new inrequests
version 2.4.2; if you still have to use an older version, encode the JSON manually using thejson
module and post the encoded result as thedata
key; you will have to explicitly set the Content-Type header in that case:Assign the response to a value and test the attributes of it. These should tell you something useful.