The requests library is one of the important aspects of Python for making HTTP requests to a specified URL. The post() method is used when we want to send some data to the server. Then the data is stored in the Database.
POST Http Method
POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.
Know more about python post request here at Entri!
Python Requests Post
To create a POST request in Python, use the requests.post() method. The requests post() method accepts URLs. data, json, and args as arguments and sends a POST request to a specified URL.
Syntax
The syntax of requests post() example is the following.
requests.post(url, data={key: value}, json={key: value}, args)
Example –
import requests
# Making a POST request
r = requests.post(‘https://httpbin.org / post’, data ={‘key’:’value’})
# check status code for the response received
# success code – 200
print(r)
# print content of request
print(r.json())
Learn Coding in your Language! Enroll Here!
Parameters for Python Post Request
Parameter |
Description
|
URL |
It is required. The URL of the request.
|
data |
It is optional. It can be a dictionary, a list of tuples, bytes or a file object to send to the specified URL.
|
JSON |
It is optional. It is a JSON object to submit to the specified URL.
|
files |
It is optional. It is a dictionary of files to send to the specified URL.
|
allow_redirects |
It is optional. It is a Boolean to enable/disable redirection.
Default True (allowing redirects) |
auth |
It is optional. It is a tuple to enable secure HTTP authentication.
Default None |
cert |
It is optional. It is a String or Tuple specifying a cert file or key.
Default None |
cookies |
It is optional. It is a dictionary of cookies to send to the specified URL.
Default None |
headers |
It is optional. It is the dictionary of HTTP headers to send to the specified URL.
Default None |
proxies |
It is optional. It is a dictionary of the protocol to the proxy URL.
Default None |
stream |
It is optional. A Boolean value illustrates if the response should be immediately downloaded (False) or streamed (True).
Default False |
timeout |
It is optional. It is a tuple, or number, indicating how many seconds to wait for a client to make the connection and send a response. The default argument is None which means the request will continue until the connection is closed or lost.
|
verify |
It is optional. It is a Boolean or a String indication to check the server’s TLS certificate or not.
Default True |
Key points about Post request
- POST requests have no restraints on data length. It can be whatever you want.
- POST requests do not live in the browser history.
- POST requests are never cached.
- POST requests cannot be bookmarked.
Advantages of using the POST Method
- It is more secure than GET because user-entered information is never visible in the URL query string or in the server logs.
- There is a much larger limit on the amount of data that can be passed and one can send text data as well as binary data (uploading a file) using POST.
Disadvantages of using the POST Method
- Since the data sent by the POST method is not visible in the URL, so it is not possible to bookmark the page with a specific query.
- POST requests are never cached
- POST requests do not remain in the browser history.
Learn Coding in your Language! Enroll Here!
Entri provides video classes as well on various important topics by the excellent faculties. It also gives you access to clarify your doubts. Entri provides an excellent platform with full-length mock tests including previous year’s question papers.