I have already told you about the get method of the requests module. In this article, we will introduce another commonly used method in the requests module, the post method.
The form of the post method is more complicated than that of get. At this time, because the post needs to provide some data information when submitting, for use, the two methods are basically similar.
Note that the query string (name/value pair) is sent in the HTTP message body of the POST request:
Below we use an example to briefly understand the use of the post method
First of all, we need to understand, the following website
www.httpbin.org
The httpbin website can test various information of HTTP requests and responses, such as cookies, ip, headers and login verification, etc., and supports GET, POST and other methods, which is very helpful for web development and testing. It is written in Python + Flask and is an open source project. You can use the online website directly, or you can build a test environment locally by yourself
The following is the open source address
After getting familiar with this, we start to formally write the code
# coding=utf-8 __Author__ = "susmote" import requests post_data = {'user': 'susmote', 'passwd': '123456'} resp_post = requests.post('http://httpbin.org/post', data=post_data) print(resp_post.json())
A very simple piece of code, first defines a dictionary for incoming data, then calls the post method in requests, transfers the data to httpbin for response testing, and then saves the returned result as resp_post
Finally, through the json method, convert it into a dictionary for observation
Let's run this code on the command line
It can be seen that the httpbin website server correctly parses the submitted post request and returns some data at the same time
About the post method in the requests module is probably these