Python development [module]: Requests (2), pythonrequests
Common 4-medium post Requests in the Requests Module
The HTTP Protocol specifies that the data submitted by POST must be placed on the message subject (entity-BodyBut the Protocol does not specify the encoding method required for data. The following are four common encoding methods:
1. application/x-www-form-urlencoded
This should be the most common way to submit data through POST. If the native form of the browser does not set the enctype attribute, data will be submitted in application/x-www-form-urlencoded mode. The request is similar to the following:
import requestsimport jsonCONFIG = { 'url': 'http://192.168.90.10:8888/', 'headers': {'Content-Type': 'application/x-www-form-urlencoded'}}data = {'content': 'hello', 'digital': '0', 'punctuate': '1', 'engModel': '2'}url = CONFIG['url']headers = CONFIG['headers']response = requests.post(url=url, data=data,headers=headers,timeout=1)print(response.content)
Use wirshark to capture packets and analyze the request data format
Tracking the http data stream, you can see that the requested data is spliced like this content = hello & engModel = 2 & punctuate = 1 & digital = 0
2. multipart/form-data
This is also a common POST data submission method. When using a form to upload a file, make the form's enctyped equal to this value. The following is an example:
import requestsimport jsonCONFIG = { 'url': 'http://192.168.90.10:8888/', 'headers': {'Content-Type': 'multipart/form-data '}}data = {'content': 'hello', 'digital': '0', 'punctuate': '1', 'engModel': '2'}url = CONFIG['url']headers = CONFIG['headers']response = requests.post(url=url, files=data,headers=headers,timeout=1)print(response.content)
Use wirshark to capture packets and analyze the request data format
Tracking http data streams to view the requested data
3. application/json
The Content-Type of application/json is the response header. In fact, more and more people use it as the request header to tell the server that the Message Subject is a serialized JSON string. Due to the popularity of JSON specifications, all major browsers except earlier versions of IE support JSON. stringify native, and the server language also has functions for processing JSON, so there is no trouble in using JSON.
import requestsimport jsonCONFIG = { 'url': 'http://192.168.90.10:8888/', 'headers': {'Content-Type': 'application/json'}}data = {'content': 'hello', 'digital': '0', 'punctuate': '1', 'engModel': '2'}url = CONFIG['url']headers = CONFIG['headers']response = requests.post(url=url, data=json.dumps(data),headers=headers,timeout=1)print(response.content)
Use wirshark to capture packets and analyze the request data format
Tracking http data streams to view the requested data
4. text/xml
It is a remote call specification that uses HTTP as the transmission protocol and XML as the encoding method.