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?
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 GET
s.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With