To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params
argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request.
If{'param1': 'value1', 'param2': 'value2'}
are the parameters, andhttps://somewebsite.com/
is the url
then https://somewebsite.com/?param1=value1¶m2=value2
would be our final url.
Example – Send Parameters in URL using Python Requests
In the following example, we are sending a parameter in the URL with GET request. To check if the URL is formatted correctly, you can print it from the response object using response.url
as shown in the below program.
import requests
params = {'p': '9431'}
response = requests.get('https://pythonexamples.org/',
params=params)
print(response.url)
Run https://pythonexamples.org/?p=9431
Example – When the URL gets Redirected
In the following example, we will send a parameter, where the final URL gets redirected.
import requests
params = {'p': '943'}
response = requests.get('https://pythonexamples.org/',
params=params)
print(response.url)
Run There is a post published with the id 943. Hence when you request for the resource at https://pythonexamples.org/?p=943, it gets redirected to https://pythonexamples.org/python-requests-http-get/ and hence would be our final URL in the response.
https://pythonexamples.org/python-requests-http-get/