Hello i’m using request
to perform some http calls. The is a strange behaviour, the response should contain a list of items, but i only get one item.
This is the result i get from Postman when performing the call:
{ "results": [ { "code": "sample_code", "amount": "90.00", "is_available": true, }, { "code": "sample_code1", "amount": "30.00", "is_available": false, }, { "code": "sample_code2", "amount": "333.00", "is_available": true, }] }
With this is the reponse i get in my script :
{ "results": [ { "code": "sample_code2", "amount": "333.00", "is_available": true, }] }
this is how i perform the action :
for product in sorted(products): jsonObject = requests.post(endpoint,json=payload, verify=False).json print(jsonObject)
it’s like it returns only the last element. I could not figure out why.
EDIT added request:
{ "locale": "en-GB", "products": [ { "categories": [ "T-Shirt" ] } ], "basketTotal": { "amount": "42", "currency": "EUR" }, "requestDateTime": "2021-04-16T10:53:45.770Z", "stock": { "isReplen": false } }
Advertisement
Answer
I found the problem and then the solution. Going deep reading https://docs.python-requests.org/en/latest/user/quickstart/#make-a-request, instead of giving a JSON ojbect i could send a string payload that encodes a JSON. So my code now is :
requests.post(url,headers=header, data=payload, verify=False)
and return the list of items correctly, i’ve added an header : 'Content-Type': 'application/json; charset=Latin-1
because i need it later.