LittleQ
LittleQ

Reputation: 1925

Parse url like requets.Request.url which prepared

I'm testing on some ajax requests. I've prepared a login session, wanna get request

sample input:

GET:
/ajax/uri
Params:
context:{JSON_STRING}

sample output:

url/ajax/uri?context=_QUOTED_JSON_STRING

I can get the output in this way:

r = requests.Request(url='url/ajax/uri', method='get', params={'context':JSON_STRING})
url = r.prepare().url
# next step: session.get(url)

I wonder if there's a more elegant&simple way to do the thing for I only wanna the prepared url. Any help would be appreciated.

Upvotes: 1

Views: 385

Answers (2)

Tsing
Tsing

Reputation: 46

See http://docs.python-requests.org/en/latest/user/advanced/

s.get('url/ajax/uri', params={'context':JSON_STRING})

Upvotes: 1

bakkal
bakkal

Reputation: 55448

If you just want the URL, you can encode the GET parameters with urlencode, your values can be JSON strings

>>> import urllib
>>> 'url/ajax/uri?%s' % urllib.urlencode({'key1': 'value1', 'key2': 'value2'})
'url/ajax/uri?key2=value2&key1=value1'

Upvotes: 1

Related Questions