Create url without request execution

Question:

I am currently using the python requests package to make JSON requests. Unfortunately, the service which I need to query has a daily maximum request limit. Right know, I cache the executed request urls, so in case I come go beyond this limit, I know where to continue the next day.

r = requests.get('http://someurl.com', params=request_parameters)
log.append(r.url)

However, for using this log the the next day I need to create the request urls in my program before actually doing the requests so I can match them against the strings in the log. Otherwise, it would decrease my daily limit. Does anybody of you have an idea how to do this? I didn’t find any appropriate method in the request package.

Asked By: Andy

||

Answers:

You can use PreparedRequests.

To build the URL, you can build your own Request object and prepare it:

from requests import Session, Request

s = Session()
p = Request('GET', 'http://someurl.com', params=request_parameters).prepare()
log.append(p.url)

Later, when you’re ready to send, you can just do this:

r = s.send(p)

The relevant section of the documentation is here.

Answered By: Lukasa