Reputation: 1
I am facing an issue where i need to send multiple values to parameters in the get response.
For example
API is URL =https://localhost:9090?id=1&department=hr
Now in id
I want to send 1,2,3,4
in a single request and I did like
PARAMS = {'id': [1,2,3,4],'department'='hr'}
r = requests.get(url = URL, params = PARAMS)
It is not still giving the desired response as it is showing value for only id =4
. Can anybody please help me with this?
Upvotes: 0
Views: 3134
Reputation: 1674
This is most likely a server-side issue. Check whether the server is parsing the id
field as intended or not. It would be much better if you could post the server code where the id
field is being parsed.
Upvotes: 0
Reputation: 42187
When given a sequence, requests duplicates the key as you can see in the example from the official documentations.
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get('https://httpbin.org/get', params=payload)
>>> print(r.url)
https://httpbin.org/get?key1=value1&key2=value2&key2=value3
note key2=value2
and key2=value3
, that's the standard encoding for multiple values (there are others e.g. comma or semicolon-separated values, or "magic" key names postfixed with []
as in PHP).
However depending on your server-side framework that may require using special APIs to get the values as lists, otherwise you might get just the first or just the last because the querystring is interpreted as a 1:1 dict (here you only get the last one).
So your solutions are:
Upvotes: 2
Reputation: 3731
Normally strings are send back and forth. In this case you may try:
PARAMS = {'id': '1,2,3,4','department' : 'hr'}
At server side you need to decode ID be using id.split(','). Then you get the four independent values again.
Upvotes: 0