Python Requests - pass parameter via GET

70,095

Here's the relevant code to perform a GET http call from the official documentation

import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)

In order to adapt it to your specific request:

import requests
payload = {'q': 'food'}
r = requests.get('http://httpbin.org/get', params=payload)
print (r.text)

Here's the obtained result if I run the 2nd example:

python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"http://httpbin.org/get?q=food"}
Share:
70,095
Cody Raspien
Author by

Cody Raspien

Updated on July 23, 2022

Comments

  • Cody Raspien
    Cody Raspien almost 2 years

    I am trying to call an API and pass some parameters to it.

    My endpoint should look like:

    https://example.com?q=food food is the parameter

    import requests
    parametervalue = "food"
    r = requests.get("https://example.com/q=", parametervalue)
    

    When I do print r.url, I do not get the correct result - the value is not passed.

    Updated:

    Error:

      r = requests.get('https://example.com', params={'1':parametervalue})
      File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
      return request('get', url, **kwargs)
    

    Update:

    Answer posted below. Thank you.

    Note: The SSL error mentioned in the post was due to me running Python 2.7. I used python3 and it fixed the issue.