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:

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!