Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing comma separated values in request.get python

Hi I am trying to read from a restful api

http://localhost:1010/productfamily?date=2015-08-28&list=abc,xyz

I am passing the values like this

url = 'http://localhost:1010/productfamily'
sdate = '2015-08-28'
plist = 'abc,xyz'
params = (('date', sdate ), ('list', plist))
requests.get(url, params)

I am not getting the wanted output. How do i pass a comma separated string in this way?

It works when i pass it like this

requests.get(url+?'date='+sdate'&lisy='plist)
like image 468
user2728024 Avatar asked Feb 24 '26 03:02

user2728024


1 Answers

To pass multiple values for the same key, pass in a dict with an iterable as its value

url = 'http://localhost:1010/productfamily'
params = {'sdate': '2015-08-28'}
params['plist'] = ('abc', 'xy')
requests.get(url, params)

EDIT:

The OP did ask for a comma separated value list, and as somewhatoff points out in the comments, this code produces multiple key=value parameters. That being said, the OP also states that they are "reading" from an API and issuing GET requests, indicating that this is not a service that they are creating. While there is NO spec for how to provide multiple values, my experience is that "most" server frameworks will accept this format for multiple values, and I've not seen the CSV format required "in the wild."

Wikipedia supports this position here: https://en.wikipedia.org/wiki/Query_string#Web_forms

Also see this answer for more information about the behavior of specific web servers: https://stackoverflow.com/a/1746566/4075135

like image 123
ZachP Avatar answered Feb 27 '26 01:02

ZachP