api - How to send an array using requests.post (Python)? "Value Error: Too many values to unpack" -
i'm trying send array(list) of requests wheniwork api using requests.post, , keep getting 1 of 2 errors. when send list list, unpacking error, , when send string, error asking me submit array. think has how requests handles lists. here examples:
url='https://api.wheniwork.com/2/batch' headers={"w-token": "ilovemyboss"} data=[{'url': '/rest/shifts', 'params': {'user_id': 0,'other_stuff':'value'}, 'method':'post',{'url': '/rest/shifts', 'params': {'user_id': 1,'other_stuff':'value'}, 'method':'post'}] r = requests.post(url, headers=headers,data=data) print r.text # valueerror: many values unpack
simply wrapping value data in quotes:
url='https://api.wheniwork.com/2/batch' headers={"w-token": "ilovemyboss"} data="[]" #removed data here emphasize change quotes r = requests.post(url, headers=headers,data=data) print r.text #{"error":"please include array of requests make.","code":5000}
you want pass in json encoded data. see api documentation:
remember — post bodies must json encoded data (no form data).
the requests
library makes trivially easy:
headers = {"w-token": "ilovemyboss"} data = [ { 'url': '/rest/shifts', 'params': {'user_id': 0, 'other_stuff': 'value'}, 'method': 'post', }, { 'url': '/rest/shifts', 'params': {'user_id': 1,'other_stuff': 'value'}, 'method':'post', }, ] requests.post(url, json=data, headers=headers)
by using json
keyword argument data encoded json you, , content-type
header set application/json
.
Comments
Post a Comment