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
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)
Do a string concatenation.
url = 'https://exampleurl/'
path = 'home'
url += path
url = 'https://exampleurl/home'
You can simply use f-string
url = f"https://exampleurl/{path}"
response = request.get(url, params=query)
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
You can do string format on the url to fill in the path params.
url = 'https://exampleurl/{path_param}'
api_call = url.format(path)
Do a string concatenation.
url = 'https://exampleurl/'
path = 'home'
url += path
url = 'https://exampleurl/home'
You can simply use f-string
url = f"https://exampleurl/{path}"
response = request.get(url, params=query)