Python Requests Post Method

Python is a popular programming language that includes a number of libraries for handling HTTP requests. The requests library is the one of the most extensively used HTTP library for Python that provides a simple interface for client and server requests. It provides a powerful interface for making HTTP requests such as GET, POST, PUT, DELETE, and others. This article delves into the details of the Python Requests POST method.

What is post method and why do we use it?

Python Requests is a powerful HTTP library which provides a simple and elegant way to interact with web services and APIs, making it easier to send and receive data over the internet. POST method is commonly used method for sending data to a server and working with APIs. It is typically used to submit forms or upload files.

Setting up environment for Python Requests Post Method

Here is a demonstration of setting an environment to use the python requests library:

Download & Install Python

First of all, download and install the latest version of python from the official website https://www.python.org/downloads/. Follow the on-screen instructions to select the proper operating system and architecture. You can skip this step, if you’ve already installed python in your local device.

Install Request Module

The requests module is not a built-in module in Python, so it must be installed. You can install the Python Requests library using pip, the Python package installer. Simply open a terminal or command prompt and execute the following command line:

pip install requests

Import Request Module

Once you have installed the request library using the pip command in your directory, you can import it into your Python script using the following line of code:

import requests

Your environment is now set up for using Python Requests Post Method. You can now start using requests module to send POST requests to websites and APIs.

SYNTAX OF Python Requests Post Method

Now lets write our first program which uses Post Method call.

The requests.post() method is used to send a post request to a specified URL with optional parameters.

requests.post(url, data=None, json=None, headers=None, cookies=None, auth=None, timeout=None)

Let’s dive into the details of each parameter:

  • url – Specifies the URL to which the request is sent.
  • data – As the name implies, the data parameter consists of data which is passed to the specified url in the form of either key-value pairs or a string. This data can be in the form of a dictionary, tuples, string object. You can even pass a file type object.
  • json – This parameter is used to send JSON-encoded data which is passed into the request body.
  • headers – This parameter is used to pass additional information about the request being made. They are a dictionary-like objects which contain the header name along with the values.
  • cookies – This parameter is used to send cookies in the form of dictionary which contains the cookie name and the cookie value into the request body.
  • auth – This parameter is used to pass the authentication parameters i.e., username and password along with the request.
  • timeout – It represents the amount of time to wait for a response from the server in seconds before timeout.

The requests.post() method returns a Response object containing the server’s response to the request. Now, lets understand the use of POST method and the use of the above parameters along with examples.

Sending JSON DATA WITH POST METHOD

We will be using “https://httpbin.org” url to send JSON data. It is a website that offers a straightforward HTTP request/response service which is primarily used for testing and troubleshooting. It provides a set of endpoints for performing various HTTP methods and receiving a well-formatted response.

The ‘https://httpbin.org/post‘ is an endpoint that is set up by httpbin to receive data from a form or a file.

import requests

url = 'https://httpbin.org/post'

data = {'FirstName': 'Peter', 'LastName': 'John', 'age': 20, 'Designation': 'IT Engineer'}

headers = {'Content-Type': 'application/json'}

response = requests.post(url, json=data, headers=headers)

Now, print the response variable.

print(response)
Output: <Response [200]>

In the output, the Response is an object of the Response class and the 200 represents status code. The Response class has various useful attributes and provides multiple methods for getting response-related information, such as the response status code, headers etc. You can get all the methods of a response class by printing the following command:

print(dir(response))
Output: ['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']

The above command will return a list of all the methods available for the Response object. You can use them to retrieve the information you need from the response such as response.json() returns the content as JSON. Another commonly used method is response.text. It parses the server response in the form of a string.

The status code field is a three-digit number which is basically a HTTP code returned by the server. It represents the status of the request response if it is successful or not. A status code of 200 indicates that the request was successful, while a status code of 404 means that the requested resource was not found. The status code field can also be used for error handling.

The Url attribute returns the URL that was actually used in the HTTP request. You can use this attribute for debugging purposes to ensure that the request uses the correct URL.

Lets consider an example. The data is passed in the data parameter in the form of a dictionary to the requests.post() method. This data is then sent to the specified URL within the Post request. The request_response variable, on the other hand, stores the server response.

import requests
url = "https://httpbin.org/post"
data = {"username": "aimenjabeen", "password": "xyz"}
request_response = requests.post(url, data=data)
print(request_response.status_code)
Output: 200

In the above example, the status code is 200, which indicates that the response is successful. Now, execute the following command:

print(response.text)
Output:  {
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "password": "xyz", 
    "username": "aimenjabeen"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "33", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.27.1", 
    "X-Amzn-Trace-Id": "Root=1-643a5b6c-676ce01e4cf441e0552e225d"
  }, 
  "json": null, 
  "origin": "34.172.109.118", 
  "url": "https://httpbin.org/post"
}

The code above print out the content of the response from the server in the form of a string.

print(request_response.url)
Output: https://httpbin.org/post

In this example, the URL "https://httpbin.org/post" is printed as a response that is specified in the url parameter.

Sending post requests with HEADERS

As discussed above that the headers are used to pass the additional information along with the request body. Some of the common headers are:

  • User-Agent: This header usually contains information about the client who is making the request such as the name and version of the browser used by the client.
  • Content-Type: It specifies the format of the data which is to be sent in the request body. For JSON data, the Content-Type header will be set to “application/json.”
  • Authorization: This header is used to provide authentication credentials along with the request. The header may differ depending on the type of authentication used.
  • Accept: This header defines what kind of response the client will accept. If it is set to “application/json”, the server will respond with a JSON response.

HTTP headers are passed in the form of a dictionary to the headers argument in a POST request where each key represents a header name, and the value is the corresponding header value. In the example code below, the headers parameter sets two headers: HTTP_POST and Content-Type. It is set to entechin in the HTTP_HOST header, and text/php in the Content-Type header.

import requests

url = "https://httpbin.org/post"

dic_data = {"username": "aimenjabeen", "password": "xyz"}

obj = requests.post(url, data = dic_data, headers = {"HTTP_HOST": "entechin", "Content-Type":"text/php"})

print(obj.headers)
Output: {'Date': 'Sun, 09 Apr 2023 21:09:44 GMT', 'Content-Type': 'application/json', 'Content-Length': '501', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}

The response from the server is stored in the obj variable, and the obj.headers attribute is used to print the response headers. The following code can be useful for inspecting the headers returned by the server and processing them further in the Python script.

Sending post requests with an authentication

Authentication refers to the process of giving a user permissions to access a particular system, network or device to allow only specific users to access data from URL. Typically, authentication data is sent by an Authorization header or a server-defined custom header. For HTTP basic authentication, the post requests provides an auth parameter which is used to pass the authentication credentials.

The most basic example of authentication is user identification which is frequently determined via access control using credentials like username and password. A tuple containing the username and password is passed to the ‘auth’ header.

import requests

url = "https://httpbin.org/post"

params = {'name': 'Andrew Parker',  'job': 'IT Department'}
 
res = requests.post(url, data = params, auth = ('username', 'password'))

print("Status code: ", res.status_code)

print(res.text)
Output: 
Status code:  200
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "job": "IT Department", 
    "name": "Andrew Parker"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", 
    "Content-Length": "36", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.27.1", 
    "X-Amzn-Trace-Id": "Root=1-6447d4c7-079064f30daa74f831506861"
  }, 
  "json": null, 
  "origin": "104.155.145.17", 
  "url": "https://httpbin.org/post"
}

The above example prints the response’s status code using the res.status_code attribute which returns 200 in the output depicting that the request is successful. The response also contain information about the request, including the data sent and the authentication information.

Sending post request with timeout handle

The Python requests library does not by default set a timeout for any requests that it sends. It may help to avoid unexpected problems, it may also cause your request to run indefinitely. Python requests library won’t time out unless specifically directed to do so. Therefore, it is important to set a timeout to stop unexpected behaviour.

The timeout argument can be used to provide a timeout in an HTTP request sent using the requests library. An integer or a floating point number is passed to this argument which specifies the time in seconds. You can use a try-except block to catch any exceptions that might be raised while making the request. The code below demonstrates how you can send post request with timeout handle.

Firstly, the code tries to connect with a timeout of 0.1 seconds, and if a timeout exception is raised, the code will print “Connection timeout”. However, if a connection error occurs, the code will pass and continue running.

#import request library
import requests

#request from web browser
URL = "https://httpbin.org/post"

#entering a data
auth = {"username": "aimenjabeen", "password": "xyz"}

#try-except
try:
   requests.post(URL, data=auth, timeout=0.1)

except requests.Timeout:
   print("Connection timeout")

except requests.ConnectionError:
   pass
Output: Connection timeout

In this example, the requests.Timeout exception is raised if the request takes too long to complete and print “Connection timeout” on the output screen. On the other hand, if there is a network connection problem, requests.ConnectionError exceptions is raised. It can be useful for handling errors that might occur while making HTTP requests and providing a better user experience by displaying appropriate error messages.

Conclusion

The post method is one of the best ways to send data to a server without having to write separate code for each bit of information you want to send. This post covers how to send the POST request along with authentication, headers, time-out handle etc. Different parameters are discussed along with the examples. Still if you have any queries or find any concept difficult to understand, let us know in the comments or click contact us page.

Leave a Comment

Your email address will not be published. Required fields are marked *