Ling
Ling

Reputation: 911

how to split Python requests get with parameter?

I need help on how do I split the parameter from an url in when using python requests get.

Assuming I have this url

https://blabla.io/bla1/blas/blaall/messages?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D

and I did requests.get by

_get = requests.get("https://blabla.io/bla1/blas/blaall/messages?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D", headers={"Authorization":"MyToken 1234abcd"})

I checked with _get.url and it return

u'https://blabla.io/bla1/blas/blaall/messages?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D'

Then I tried with the following to split the parameter

url = "https://blabla.io/bla1/blas/blaall/messages"
query = {"data[]":[{"limit_count":100, "limit_size":100}]}
headers = {"Authorization":"MyToken 1234abcd"}
_get = requests.get(url, params=query, headers=headers)

_get.url return the following result

u'https://blabla.io/bla1/blas/blaall/messages?data%5B%5D=limit_count&data%5B%5D=limit_size'

without 100 and 10000

In this kind of url --> https://blabla.io/bla1/blas/blaall/messages?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D, how exactly to split its parameter?

Thank you for your help.

Upvotes: 1

Views: 6404

Answers (3)

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8950

As far as I know, you can't use the requests library to parse URLs.

You use that to handle the requests. If you want a URL parser, use urllib.parse instead.

Upvotes: 0

AChampion
AChampion

Reputation: 30288

So you are looking for:

data={"limit_count":100,"limit_size":1000}

as your query params.

Unfortunately, requests will not flatten this nested structure, it treats any Iterable value as multiple values for the key, e.g. your nest dictionary is treated like:

query = {'data': ['limit_count', 'limit_size']}

Which is why you don't see 100 and 1000 in the end result.

You will need to flatten it into a string. You can use json.dumps() to create the required string (double quotes vs. single quotes, compact). Then requests will do the required URL encoding, e.g.:

In []:
data = {'limit_count': 100, 'limit_size': 1000}
query = {'data': json.dumps(data, separators=(',', ':'))}
request.get('http://httpbin.org', params=query).url

Out[]:
'http://httpbin.org/?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D'

Upvotes: 1

Rahul
Rahul

Reputation: 11560

from urllib.parse import urlsplit, parse_qs
import requests
url = "https://blabla.io/bla1/blas/blaall/messages?data=%7B%22limit_count%22%3A100%2C%22limit_size%22%3A1000%7D"
query = urlsplit(url).query
params = parse_qs(query)
headers = {"Authorization":"MyToken 1234abcd"}
_get = requests.get(url, params=params, headers=headers)

Upvotes: 0

Related Questions