Add path parameter in requests

Question:

If I would have:
https://exampleurl/{path} an URL like this, then what functionality in the requests module should I use to fill in the path? I know that I can pass in something like:

query = {'q0':'path_params','q1':'hello', 'q2':'world'}

and pass that into requests.get(url, params=query). But that produces something like this:

https://exampleurl/?q1=hello&q2=world

instead of https://exampleurl/path_params?q1=hello&q2=world

Asked By: KMNJB

||

Answers:

You can do string format on the url to fill in the path params.

url = 'https://exampleurl/{path_param}'
api_call = url.format(path)
Answered By: Sarvesh Dubey

Do a string concatenation.

url = 'https://exampleurl/'
path = 'home'
url += path
url = 'https://exampleurl/home'
Answered By: Ram

You can simply use f-string

url = f"https://exampleurl/{path}" 

response = request.get(url, params=query)

      
Answered By: Ernest Asare
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.