Sending Data in Requests: Payloads, Headers, and Parameters

Feb 3, 2024 ยท 2 min read

The Python requests module allows you to easily send HTTP requests to APIs and websites. When making requests, you'll often need to send data to the server in order to create resources, update data, run searches etc. There are a few ways to attach data to your requests in Python.

JSON Payload

The most common way to send data is as a JSON payload. This attaches the data to the body of the request. For example:

import requests

data = {'key1': 'value1', 'key2': 'value2'}

response = requests.post('https://api.example.com/create', json=data)

The json parameter will automatically serialize the Python dict to JSON and add the correct Content-Type: application/json header to the request.

Form-Encoded Data

For posting form data, you can use the data parameter instead:

data = {'name': 'John', 'email': 'john@email.com'}

response = requests.post('https://api.example.com/profile', data=data)

This will send form-encoded data with the Content-Type: application/x-www-form-urlencoded header.

Request Parameters

For GET requests, you can attach data as query parameters using the params parameter:

import requests

params = {'search': 'python'}

response = requests.get('https://api.example.com/search', params=params) 

This appends the data to the URL as ?search=python.

In summary, json attaches JSON payloads, data sends form-encoded data, and params sets query parameters for sending data in Python requests.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: