Go: HTTP protocol---multipart/form-data request analysis

Source: Internet
Author: User

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
    1. 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
  1. --${bound}
  2. Content-disposition:form-data; name="Filename"
  3. Http.pdf
  4. --${bound}
  5. Content-disposition:form-data; name="file000"; filename="http protocol detailed. pdf"
  6. Content-type:application/octet-stream
  7. %pdf-1.5
  8. File content
  9. %%eof
  10. --${bound}
  11. Content-disposition:form-data; name="Upload"
  12. Submit Query
  13. --${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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.