Sending Data in GET Requests with Python Requests

Feb 3, 2024 ยท 2 min read

The Python Requests library provides a simple way to send GET requests with data using the requests.get() method.

Here is an example GET request that sends some data to a URL:

import requests

url = 'https://api.example.com/data'

payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.get(url, params=payload)

print(response.url)
# https://api.example.com/data?key1=value1&key2=value2

As you can see, we pass a dictionary payload to the params parameter of requests.get(). This dictionary contains the key-value pairs of data we want to send.

Requests encodes this dictionary into a query string that is appended to the URL. You can see the full URL with the query string printed out.

This is perfect for sending non-sensitive data like filters, pagination options, etc. in a GET request. The data shows up right in the URL.

Some tips when sending data in GET requests:

  • Don't send large payloads of data to avoid hitting URL length limits
  • Encode any spaces, slashes, etc to avoid issues (Requests does this encoding automatically)
  • Consider using POST requests for sensitive data or very large payloads
  • Example fetching paginated data:

    params = {'page': 2, 'per_page': 25}  
    response = requests.get(url, params=params)

    Here we fetch page 2 with 25 results per page. The page and per_page options are sent right in the URL parameter.

    So in summary, Requests makes it very easy to send additional data in your GET requests using a params dictionary. Give it a try next time you need to send filters or options!

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!