What is the '-' in multipart/form-data?
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 '='?
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--
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
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).
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.