In the specification of the HTTP protocol, the HTTP request is divided into three parts: the status line, the request header, and the request body. When sending an HTTP request, you need to indicate the method sent in the request header, including options,, POST, PUT, DELETE, TRACE, CONNECT. Where get and post are most commonly used. About the difference between post and get, the knowledge base has been described by students, here is the main introduction of Multipart/form-data request specifically what is the matter.
In a normal HTML Form POST request, it uses content-length to indicate the length of the content in the header message. The header information is one line, and the empty line is the body, the "content" (entity). Its content-type is application/x-www-form-urlencoded, which means that the message content is URL-encoded, just like the querystring in the URL at GET request. Take the request of the Sogou browser extension upgrade as an example:
In the early HTTP post is not supporting file upload, programming development brings a lot of problems. Therefore, in the RFC 1867-form-based file Upload in HTML, the type that is used to support the upload of files is added. That is, the Content-type type expands the multipart/form-data to support sending binary data to the server. So when sending a POST request, the form <form> properties enctype have two values to choose from, and this property manages the MIME encoding of the form:
- application/x-www-form-urlencoded (default value)
- Multipart/form-data
In fact, the form form, when you do not write the Enctype property, also adds the Enctype property value to it by default, which is Enctype= "application/x-www-form-urlencoded".
So what are the characteristics of the Multipart/form-data request?
1, the basic method of Multipart/form-data is post
2. Multipart/form-data differs from the normal POST method: request header, request body.
3, the Multipart/form-data request header must contain a special header information: Content-type, and its value must also be specified as Multipart/form-data, It is also necessary to specify that a content separator is used to split the content of multiple posts in the request body, such as the contents of the file and the text content naturally need to be separated, otherwise the receiver will not be able to parse and restore the file.
4, Multipart/form-data the request body is also a string, but unlike the normal POST request body is the way it is constructed, post is a simple Name=value value connection, and multipart/ Form-data is a constructor that adds content such as delimiters.
Intercepting the contents of a request package sent via Fiddler
this time 12306 During the test process of booking a ticket, the client submits the information to the server by Multipart/form-data when testing the interface for submitting the reservation form. In order to improve the testing efficiency, Python is used to construct the network request to test the interface. The specific code is as follows:
# Coding=gbkimport Urllib2;import JSON; Def post_data (): parameters = {' id ': ', ' user ': {' username ': ', ' Password ': '}, ' query ': {' fromstation ': ' Beijing ', ' fromstationtext ': ' Shanghai '}} jdata = json.dumps (Parameters) Post_multipart (' http://extention.ie.sogou.com/ Mm_12306/prebook ', jdata) def post_multipart (URL, fields): content_type, BODY = encode_multipart_formdata ("Data ", fields) req = Urllib2. Request (URL, body) req.add_header ("User-agent", "mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.63 safari/537.36 SE 2.X METASR 1.0) ") Req.add_header (" Accept "," */* ") Req.add_header (" Accept-language "," zh-cn,zh;q=0.8 ") Req.add_header (" Accept-encoding "," GZIP,DEFL Ate,sdch ") Req.add_header (" Connection "," Keep-alive ") Req.add_header (" Content-type ", Content_Type) Req.add_h Eader ("User-agent1", "Sogoumse") Try:response = Urllib2.urlopen (req) the_page = response.Read (). Decode (' Utf-8 ') print the_page return the_page except Urllib2. Httperror, E:print E.code pass except URLLIB2. Urlerror, E:print str (e) Pass def encode_multipart_formdata (key, value): boundary = '----------this _is_the_boundary_$ ' CRLF = ' \ r \ n ' L = [] l.append ('--' + boundary) l.append (' Content-disposition:form-data ; Name= "%s" '% key "L.append (") l.append (value) l.append ('---' + boundary + '--') l.append (') body = Crlf.join (L) content_type = ' multipart/form-data; boundary=%s '% boundary return content_type, body if __name__ = = ' __main__ ': Post_data ();
Python writes automation constructs Multipartform-data sends the request