Python requests - POST request with headers and body
Last Updated :
09 Aug, 2024
HTTP headers let the client and the server pass additional information with an HTTP request or response. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format.
Request with headers
Requests do not change its behavior at all based on which headers are specified. The headers are simply passed on into the final request. All header values must be a string, bytestring, or Unicode. While permitted, it’s advised to avoid passing Unicode header values. We can make requests with the headers we specify and by using the headers attribute we can tell the server with additional information about the request.
Headers can be Python Dictionaries like, { “Name of Header”: “Value of the Header” }
The Authentication Header tells the server who you are. Typically, we can send the authentication credentials through the Authorization header to make an authenticated request.
Example:
Headers = { “Authorization” : ”our_unique_secret_token” }
response = request.post(“https://example.com/get-my-account-detail”, headers=Headers)
Request Object StructureRequest with body
POST requests pass their data through the message body, The Payload will be set to the data parameter. data parameter takes a dictionary, a list of tuples, bytes, or a file-like object. You’ll want to adapt the data you send in the body of your request to the specified URL.
Syntax:
requests.post(url, data={key: value}, json={key: value}, headers={key:value}, args) *(data, json, headers parameters are optional.)
Given below are few implementations to help understand the concept better.
Example 1: Sending requests with data as a payload
Python
import requests
url = "https://httpbin.org/post"
data = {
"id": 1001,
"name": "geek",
"passion": "coding",
}
response = requests.post(url, json=data)
print("Status Code", response.status_code)
print("JSON Response ", response.json())
Output:

Example 2: Sending requests with JSON data and headers
Python
import requests
import json
url = "https://httpbin.org/post"
headers = {"Content-Type": "application/json; charset=utf-8"}
data = {
"id": 1001,
"name": "geek",
"passion": "coding",
}
response = requests.post(url, headers=headers, json=data)
print("Status Code", response.status_code)
print("JSON Response ", response.json())
Output:
Similar Reads
GET and POST Requests in GraphQL API using Python requests In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body. What
9 min read
GET and POST Requests Using Python This post discusses two HTTP (Hypertext Transfer Protocol) request methods  GET and POST requests in Python and their implementation in Python. What is HTTP? HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a cli
7 min read
response.headers - Python requests The response.headers object in Python's requests library functions as a special dictionary that contains extra information provided by the server when we make an HTTP request. It stores metadata like content type, server details and other headers, such as cookies or authorization tokens. The keys in
3 min read
response.headers - Python requests The response.headers object in Python's requests library functions as a special dictionary that contains extra information provided by the server when we make an HTTP request. It stores metadata like content type, server details and other headers, such as cookies or authorization tokens. The keys in
3 min read
Http Request methods - Python requests Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and serv
7 min read
Http Request methods - Python requests Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and serv
7 min read