What is the '-' in multipart/form-data?

前端 未结 4 1335
刺人心
刺人心 2020-12-30 04:06

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=-----...---boundaryNumbe

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 04:47

    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
    
    Second part (HTML).
    --OMGWTFPLZDIEKTHX--
    

提交回复
热议问题