Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a list in python requests GET

I'm trying to GET some data from the server. I'm doing a GET with the python requests library:

my_list = #a list ['x', 'y', 'z']
payload = {'id_list': my_list}
requests.get(url, params=payload)

my server accepts a url: https://url.com/download?id_list

but when I send this get request, I get an error:

<h1>400 Bad Request</h1> The server cannot understand the request due to malformed syntax. <br /><br /> Got multiple values for a parameter:<br /> <pre>id_list</pre>

I saw the log and the request looks like this:

url/download?id_list=x&id_list=y&id_list=z

how can I fix this?

like image 855
duxfox-- Avatar asked Mar 07 '16 18:03

duxfox--


2 Answers

Well, there's actually nothing to fix, the issue being on the server side! The way requests handle list per default is the way it should be handled.

But the real question you should be asking is what does the server side's API expect?

And once you know that you can mimic it using requests.

For example, it's likely to be that the server is expecting a json string as argument of the id_list parameter. Then you could do:

payload = {'id_list': json.dumps(my_list)}

But it can be that the server side expects a comma separated list:

payload = {'id_list': ','.join(my_list)}

So it's up to you to read the documentation (or hack your way around) of the API you try to communicate with.


N.B.: as @bob_dylan suggests you can try with requests.get(url, json=payload) but afaict, json payloads are usually used in POST queries, rarely in GETs.

like image 191
zmo Avatar answered Sep 18 '22 09:09

zmo


A good way to solve this problem is by using request.GET.getlist() on the server.

It allows you to use something like this:

import requests
requests.get(url, data={'id_list': ['x', 'y', 'z']})

And make it on the server:

request.GET.getlist('id_list')

Returning exactly what you want:

>>> ['x', 'y', 'z']

It will also return a list when you send only one item.

like image 20
ruhanbidart Avatar answered Sep 20 '22 09:09

ruhanbidart