17
votes

I want to ask a question about the multipart/form data. I find the http header of multipart post and the Content-Type: multipart/form-data; boundary=-----...---boundaryNumber. I want to ask, how many of '-' between the boundaryNumber and '='?

5

5 Answers

38
votes

Not a single - is mandatory. You can have any number of them. It is actually a mystery to me why user-agents tend to add so many. It is probably traditional because in the old days, when people still regularly looked at the actual protocol traffic, it provided some nice visual separation. Nowadays it is pointless.

Note however, that when you use the boundary in the stream, it must be prefixed by two hyphens (--). That’s part of the protocol. Of course, the fact that most user-agents use lots of hyphens in their boundary makes this very hard to see by example.

Furthermore, the last boundary (which marks the end of the message) is prefixed and suffixed by two hyphens (--).

So in summary, you could call your boundary OMGWTFPLZDIEKTHX, and then your traffic could look like this:

Content-Type: multipart/form-data; boundary=OMGWTFPLZDIEKTHX

--OMGWTFPLZDIEKTHX
Content-Type: text/plain

First part (plain text).
--OMGWTFPLZDIEKTHX
Content-Type: text/html

<html>Second part (HTML).</html>
--OMGWTFPLZDIEKTHX--
2
votes

The number of dashes depends on how many you want there. It can be zero, if you like -- it's just that more dashes makes the boundary more obvious.

The boundary consists of a line containing two dashes plus everything after "boundary=". So if your header said boundary=ABC, the boundary looks like

--ABC
1
votes

In your boundary definition, no hyphens are required. When using that boundary to separate two distinct body parts, you must start with two hyphens, followed by your previously-defined boundary string.

This is explained in RFC 1341 (MIME), and you can find additional information there in the Multipart section (as linked).

1
votes

It is completely arbitrary.

The point of the boundary is to define the beginning and ending of your data. It does not matter what it is, as long as it is not part of the content.

0
votes

Multipart/form-data media type can be used by a wide variety of applications and transported by a wide variety of protocols as a way of returning a set of values as the result of a user filling out a form.

Multipart/form-data follows the model of multipart MIME data streams. A multipart/form-data body contains a series of parts separated by a boundary.

Example of multipart/form-data response: enter image description here

There are four important fields which we are important in response:

-<<boundary_value>>

Content-Disposition: form-data; name="<<field_name>>"

Content-Type: type of the data

<<field_value>>

The "Boundary" Parameter is one of the clue in the in multipart response:

As with other multipart types, the parts are delimited with a boundary delimiter, constructed using CRLF, "--", and the value of the "boundary" parameter. The boundary is supplied as a "boundary" parameter to the multipart/form-data type. The boundary delimiter MUST NOT appear inside any of the encapsulated parts, and it is often necessary to enclose the "boundary" parameter values in quotes in the Content-Type header field. Resource - https://datatracker.ietf.org/doc/html/rfc7578