Python requests external POST requests, four common request bodies
The HTTP Protocol specifies that the data submitted by POST must be placed in the message body, but the Protocol does not specify the encoding method required for the 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 (unrelated request headers are omitted in this article ):
POST http://www.example.com HTTP/1.1 Content-Type:application/x-www-form-urlencoded;charset=utf-8title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
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:
POST http://www.example.com HTTP/1.1Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA------WebKitFormBoundaryrGKCBY7qhFd3TrwAContent-Disposition: form-data; name="text"title------WebKitFormBoundaryrGKCBY7qhFd3TrwAContent-Disposition: form-data; name="file"; filename="chrome.png"Content-Type: image/pngPNG ... content of chrome.png ...------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
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.
4. text/xml
It is a remote call specification that uses HTTP as the transmission protocol and XML as the encoding method.
So how does the post request transmit the request body when Python calls an external http request? To be honest, I have only practiced [2, multipart/form-data] and [3, application/json]
I. multipart/form-data
Use the python poster module to install poster: pip install poster
Code:
From poster. encode import multipart_encode from poster. streaminghttp import register_openers url = "http://www.example.com" body_value = {"package": "com. tencent. lian "," version_code ":" 66 "} register_openers () datagen, re_headers = multipart_encode (body_value) request = urllib2.Request (url, datagen, re_headers) # If there is request header data, then add the request header request. add_header (keys, headers [keys]) result = urllib2.urlopen (request ). read ()
2. application/json
import jsonurl = "http://www.example.com"body_value = {"package": "com.tencent.lian","version_code": "66" }register_openers()body_value = json.JSONEncoder().encode(api_body_value)request = urllib2.Request(url, body_value)request .add_header(keys, headers[keys])result = urllib2.urlopen(request ).read()