Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exact url of the requests library in python

I am using requests library of python and making a request like this

r = requests.get(solrShard1, params = solrParams)

Now i want to get the exact url the requests library is creating . I have tried using

req = requests.Request('POST',solrShard1, params = solrParams)
prepared = req.prepare()
print >>sys.stderr, "REQUEST START"
print >>sys.stderr, req.url
print >>sys.stderr, "REQUEST END"

but it only prints the url and not the exact url along with parameters and everything . How can i get the exact URL

like image 802
Evan Root Avatar asked Sep 19 '25 06:09

Evan Root


1 Answers

You are showing the url from request object, instead of the prepared object.

The call to the prepare method doesn't mutate the object itself, instead it returns the prepared object, which you are already assigning to the preparedvariable, but you are not using it anywhere.

Just change your code for the following and it should work:

print >>sys.stderr, "REQUEST START"
print >>sys.stderr, prepared.url
print >>sys.stderr, "REQUEST END"

I tried the code below and it works fine for me:

req = requests.Request('POST', 'http://google.com', params={'param1': 10})
prep = req.prepare()
print(prep.url)

>> 'http://google.com/?param1=10'
like image 121
dfranca Avatar answered Sep 21 '25 20:09

dfranca