Multipart Requests
This topic is only a brief overview of multipart requests; more information is available in the associated IETF RFC2046.
HTTP requests may declare their media type to be "multipart". This is accomplished by setting the Content-Type
header
to a MIME type whose type is multipart
(for example, multipart/form-data
). This indicates to the recipient that the body of
the request is formatted as a series of "body parts" grouped together as a single "multipart" request. Each part starts
with a boundary to indicate to the recipient the start of a new body part. The boundary is specified by setting the
boundary
property in the Content-Type
header.
Continuing the previous example of multipart/form-data
, the Content-Type
header would look like
multipart/form-data; boundary=MYBOUNDARY
. Then, the body parts contained in the request body would appears as
follows:
--MYBOUNDARY
This is my first body part.
--MYBOUNDARY
This is my second body part.
--MYBOUNDARY--
Observe that the last boundary is prefixed by --
to indicate the start of a new body part, and that the last body
part declares the end of the body by suffixing another --
.
Each "body part" is similar (but not equivalent) to a normal HTTP request. It is
formatted as a header area and a body area separated by a blank line. The header area is allowed to contain Content-
headers, but can be left blank to indicate plain text. The body area is similar to a normal request body, except that
the boundary may not appear within it - if it does, the recipient won't be able to determine the intended body
parts.
Example
The following example is a complete multipart HTTP request to a non-existent URI. Our imaginary URI expects three body
parts: a snippet of text, a JSON-formatted object, and a Rich Text Format document. The first line is the HTTP
request's Request-Line; this is not part of the request body. The next two lines are the Host
and Content-Type
headers, and are also not part of the request body.
POST /an/example/path HTTP/1.1
Host: www.mywebsite.com
Content-Type: multipart/form-data; boundary=MYBOUNDARY
--MYBOUNDARY
This is the body area of the first body part. Note that this body part has an empty header
area (indicating that the content is plain text), and that a blank line separates the
header area and body area.
--MYBOUNDARY
Content-Type: application/json
{
"ExampleMessage": "This is the body area of the second body part.",
"Notes": [
"Note that the header area of this body part contains a Content-Type header.",
"Note that the header area and the body area are separated by a blank line."
]
}
--MYBOUNDARY
Content-Disposition: form-data; name="anExample"; filename="sample.rtf"
Content-Type: application/rtf
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.16299}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang9 This is a sample RTF document. Note that in this body
part, we have specified a \i Content-Disposition\i0 header. This header allows for additional
useful information to be passed to the recipient, such as the filename of the document. At
this end of this body part, we mark the end of the multipart request.\par
}
--MYBOUNDARY--