Objective
A few days ago a friend who just contacted Python asked me how to send my Python XML format post request, and just recently looked at the HTTP request related content, so I decided to summarize.
The role of Content-typecontent-type
The HTTP request method provided by the HTTP/1.1 protocol has options,, POST, PUT, DELETE, TRACE, CONNECT. Where POST is typically used to submit data to the server. A normal post request mainly consists of a request line, a request header, and a request principal. The protocol specifies that the data submitted by the POST must be placed in the message body (entity-body), but the protocol does not specify what encoding the data must use. In fact, the developer can completely decide the format of the message body, as long as the last HTTP request satisfies the above format.
However, the data sent out, but also the service side of the success of the analysis is meaningful. General Service-side languages such as PHP, Python, etc., have built-in features that automatically parse common data formats. The server is usually based on the Content-type field in the request header (headers) to learn how the message body in the request is encoded and then parses the subject.
is an actual request message, where the first row of three elements are request method, request URL, HTTP protocol and version, called the request line, respectively, the request method, resource name, HTTP version number, starting from the second line to the penultimate line is the request header, the last line is the request body.
Content-type Types of formats
There are four common forms of Content-type :
- application/x-www-form-urlencoded(默认格式)- application/json- text/xml- multipart/form-data
Demo Code Description
We use http://httpbin.org/to do the demo this time. Httpbin is a Web site dedicated to testing HTTP requests and responses, and its GitHub open source address is Https://github.com/requests/httpbin. Author another open Source library is the famous requests.
application/x-www-form-urlencoded format
This is the most common and default data submission format for post requests. It requires an equal sign between the data name (name) and the data value (value) and is connected to the other set of Name/value values &. For example: parameter1=12345¶meter2=23456. The content of the request is formatted, in fact, this method also simplifies the client send, and simplifies the server-side acquisition, the server through the GetParameters (String name) can get to the transmitted information. This is the most common way to submit data in a Form form form.
Refer to Python implementations
import requestsdatas = {"param1": "Detector", "param2": "cnblogs"}r = requests.post("http://httpbin.org/post", data=datas)print(r.text)print(r.status_code)
We don't have content-type settings in the code, but let's take a look at the results of the Fiddler grab package.
You can see that the Content-type has been automatically populated as application/x-www-form-urlencoded .
Application/json format
Application/json this content-type as response head everybody certainly is not unfamiliar. In fact, more and more people now use it as a request header to tell the server that the message body is a serialized JSON string. Due to the popularity of the JSON specification, in addition to the low version of IE, the major browsers are natively supported json.stringify, the service-side language has a function of processing JSON, the use of JSON will not encounter any trouble.
Refer to Python implementations
import jsonimport requestsheaders = {'Content-Type': 'application/json'}datas = json.dumps({"param1": "Detector", "param2": "cnblogs"})r = requests.post("http://httpbin.org/post", data=datas, headers=headers)print(r.text)
Fiddler Clutch Results
Text/xml data format
It is a remote invocation specification that uses HTTP as the transport protocol and XML as the encoding method. The typical xml-rpc (XML Remote Procedure Call) Request data is this:
<?xml version="1.0"?><methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params></methodCall>
Refer to Python implementations
import requestsheaders = {"Content-Type": "text/xml"}datas = """<?xml version="1.0"?><methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params></methodCall>"""r = requests.post("http://httpbin.org/post", data=datas, headers=headers)print(r.text)
As you can see, the difference between us and the Application/json request is that we replace the requested content with a string in XML format, Content-type replaced by Text/xml.
Fiddler Clutch Results
Multipart/form-data data format
Multipart/form-data is primarily used for file uploads, and when we use it, we must make the form form enctype equal to Multipart/form-data. Take a direct look at a sample request, the main implementation of the upload local test.txt file:
Refer to Python implementations
import requestsfiles = {"file": open("C:/Users/Administrator/Desktop/test.txt", "rb")}r = requests.post("http://httpbin.org/post", files=files)print(r.text)
Fiddler Clutch Results
Reference documents
Https://www.cnblogs.com/aaronjs/p/4165049.html
68491509
https://www.jianshu.com/p/3c790e98ea8d
Https://www.cnblogs.com/softidea/p/5745369.html
80951327
Python implementation of "Python" Http POST request four kinds of request body