Python requests POST method
In this article, you will learn the Python requests POST method.
HTTP stands for Hypertext Transfer Protocol. HTTP was initiated by Tim Berners-Lee at CERN in 1989. This allows two nodes to communicate with each other. HTTP is a stateless data transfer protocol built upon a request/response model, client make requests to server, which then returns a response.
HTTP POST Method
The POST is one of the most common HTTP methods and is used to request data from a specified resource. In this, data of any type can be sent to the server in the body of the request message. In the POST method, we can send large amounts of data because the request parameters are appended into the body. It is comparatively more secure because the data is not exposed in the URL bar. The POST method is used for CRUD operations to create or update a resource on the server. It is used to upload files and submit web forms.
Python provides several HTTP libraries to handle GET and POST requests, like-
- httplib
- urllib
- requests
Python requests module
In this article, we are using the Python requests module.
Requests library is one of the significant aspects of Python for making HTTP requests to a specified URL. The requests module makes an HTTP request to the specified web page using Python and returns a response object. The installation process of this package is similar to the other Python packages. We can either download this from GitHub or install it using pip tool.
$ pip install requests
Python requests post() method
The requests module provides a post() method to send post requests to the specified URL. This is basically used to send some data to the server.
Syntax of requests.post()requests.post(url, data, json)
Here, url is the url of the request and is a required parameter, the data parameter can be a dictionary, a tuple, file object to send to the given url. The json is the JSON object to send to the given url.
Suppose we want to get HTML content of a website, the code will be as follows-
import requests
url = 'https://www.etutorialspoint.com/test/form.php'
req_obj = {'key': 'value'}
x = requests.post(url, data = req_obj)
print(x.text)
Python requests post headers
The HTTP Headers make the client and server pass the additional information with a HTTP request and response. It consists of its case-insensitive name followed by a colon(:) and then by its value.
import requests
url = 'https://www.etutorialspoint.com/test/form.php'
req_obj = {'key': 'value'}
x = requests.post(url, data = req_obj, headers = {"HTTP_HOST": "Etutorialspoint", "Content-Type":"text/html"})
print(x.headers)
Python requests post authentication
We can use the authentication header to pass credentials to the server. This can be possible with the auth parameter of the get method.
import requests
url = 'https://www.etutorialspoint.com/test/form.php'
req_obj = {'key': 'value'}
x = requests.post(url, data = req_obj, auth = ('username', 'password'))
print(x.status_code)
Python requests post JSON
Web applications use JSON to exchange data between a client and a server and exchange data between servers via REST API. To post a JSON data from client to server using the requests module, we can call the requests.post() method and pass the target URL as the first parameter and the JSON data in the second parameter with the json attribute.
import requests
response = requests.post('https://www.etutorialspoint.com/test/form.php', json={'id': 1, 'name': 'Priska'})
print("Status code: ", response.status_code)
print(response.json())
Related Articles
Python requests GET methodHow to convert MySQL query result to JSON in Python
How to fetch data from mongodb using python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address
Python Webbrowser
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
Factorial Program in Python
Python snake game code with Pygame
Python JSON Tutorial - Create, Read, Parse JSON file
Python convert xml to dict
Python convert dict to xml