Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querystring Array Parameters in Python using Requests

I have been trying to figure out how to use python-requests to send a request that the url looks like:

http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world' 

Normally I can build a dictionary and do:

data = {'name': 'hello', 'data': 'world'} response = requests.get('http://example.com/api/add.json', params=data) 

That works fine for most everything that I do. However, I have hit the url structure from above, and I am not sure how to do that in python without manually building strings. I can do that, but would rather not.

Is there something in the requests library I am missing or some python feature I am unaware of?

Also what do you even call that type of parameter so I can better google it?

like image 921
Buddy Lindsey Avatar asked Apr 09 '14 21:04

Buddy Lindsey


People also ask

How do you pass a parameter in a python query?

To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1&param2=value2 would be our final url.

Can we send parameters in GET request?

get() method. Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters.

What is QueryString in Python?

A query string is a convention for appending key-value pairs to a URL.

Can we use QueryString with post method?

A POST request can include a query string, however normally it doesn't - a standard HTML form with a POST action will not normally include a query string for example.


2 Answers

What u are doing is correct only. The resultant url is same what u are expecting.

>>> payload = {'name': 'hello', 'data': 'hello'} >>> r = requests.get("http://example.com/api/params", params=payload) 

u can see the resultant url:

>>> print(r.url) http://example.com/api/params?name=hello&data=hello 

According to url format:

In particular, encoding the query string uses the following rules:

  • Letters (A–Z and a–z), numbers (0–9) and the characters .,-,~ and _ are left as-is
  • SPACE is encoded as + or %20
  • All other characters are encoded as %HH hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)

So array[] will not be as expected and will be automatically replaced according to the rules:

If you build a url like :

`Build URL: http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'` 

OutPut will be:

>>> payload = {'name': 'hello', "data[]": 'hello','data[]':'world'} >>> r = requests.get("http://example.com/api/params", params=payload) >>> r.url u'http://example.com/api/params?data%5B%5D=world&name=hello' 

This is because Duplication will be replaced by the last value of the key in url and data[] will be replaced by data%5B%5D.

If data%5B%5D is not the problem(If server is able to parse it correctly),then u can go ahead with it.

Source Link

like image 24
Arvind Avatar answered Sep 18 '22 15:09

Arvind


All you need to do is putting it on a list and making the key as list like string:

data = {'name': 'hello', 'data[]': ['hello', 'world']} response = requests.get('http://example.com/api/add.json', params=data) 
like image 52
Tomer Zait Avatar answered Sep 22 '22 15:09

Tomer Zait