how to send a list in python requests GET

Viewed 38952

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?

2 Answers

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.

Related