Transferred from: http://blog.csdn.net/five3/article/details/7181521
First to understand what a multipart/form-data request is:
According to the HTTP/1.1 RFC 2616 protocol, we only request options,, POST, PUT, DELETE, Trace, and so on, so why do we have multipart/ What about the Form-data request? This is going to start from the beginning.
HTTP protocol We all know that is the provision of ASCII code transmission, based on TCP, IP protocol, the application layer specification, the specification content of the HTTP request is divided into 3 departments: status line, request header, request body. All methods and implementations are carried out around how to use and organize the three parts. In other words, it is the change, as long as we understand the components of the HTTP request, nature can strain any actual work needs and problems.
About the status line, request header, request body and other three parts of the specific content, you can refer to the official agreement document HTTP://WWW.FAQS.ORG/RFCS/RFC2616.HTML, here the main analysis of Multipart/form-data request is exactly what the matter.
Since the original method of the HTTP protocol itself does not support the Multipart/form-data request, the request is naturally evolved from these primitive methods, specifically how it evolves and looks at the following:
1, the basic method of Multipart/form-data is post, that is, by the Post method to combine the implementation of
2. The difference between Multipart/form-data and Post methods: 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. The specific header information is as follows:
[HTML]View PlainCopy
- Content-type:multipart/form-data; Boundary=${bound}
Where ${bound} is a placeholder, on behalf of our specified separator, can be arbitrarily specified, but in order to avoid and normal text duplication, try to use a bit more complex content. Example:--------------------56423498738365
4, Multipart/form-data's request body is also a string, but unlike the POST request body is how it is constructed, post is a simple Name=value value connection, and multipart/ Form-data is a constructor that adds content such as delimiters. The specific format is as follows:
[HTML]View PlainCopy
- --${bound}
- Content-disposition:form-data; name="Filename"
- Http.pdf
- --${bound}
- Content-disposition:form-data; name="file000"; filename="http protocol detailed. pdf"
- Content-type:application/octet-stream
- %pdf-1.5
- File content
- %%eof
- --${bound}
- Content-disposition:form-data; name="Upload"
- Submit Query
- --${bound}--
Where ${bound} is the delimiter in the previous header information, if the header information is set to 123, then this is also 123,; it is easy to see that the request body is composed of several identical parts: each part is a-plus delimiter, then the description of the content of the section, then a carriage return , and then describes the specifics of the information, and if the content is a file, it also contains the file name information, as well as the type of the document content. The second small part of the above is actually the structure of a file body, and finally, the end of the-----------delimiter.
In summary, you can know to send a multipart/form-data request, in fact, any support POST request tool or language can support, just want to pack a little bit.
Resources:
Baidu Encyclopedia: http://baike.baidu.com/view/9472.htm
HTTP1.1 protocol Specification: http://www.faqs.org/rfcs/rfc2616.html
Analysis tool: Httpanalyzer
Go: HTTP protocol---multipart/form-data request analysis