How To Send A List In Python Requests Get
Solution 1:
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.
Solution 2:
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.
Post a Comment for "How To Send A List In Python Requests Get"