What is the boundary in multipart/form-data?

后端 未结 3 1313
迷失自我
迷失自我 2020-11-22 08:10

I want to ask a question about the multipart/form-data. In the HTTP header, I find that the Content-Type: multipart/form-data; boundary=???.

3条回答
  •  青春惊慌失措
    2020-11-22 09:06

    multipart/form-data contains boundary to separate name/value pairs. The boundary acts like a marker of each chunk of name/value pairs passed when a form gets submitted. The boundary is automatically added to a content-type of a request header.

    The form with enctype="multipart/form-data" attribute will have a request header Content-Type : multipart/form-data; boundary --- WebKit193844043-h (browser generated vaue).

    The payload passed looks something like this:

    Content-Type: multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW
    
        -----WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Disposition: form-data; name=”file”; filename=”captcha”
        Content-Type:
    
        -----WebKitFormBoundary7MA4YWxkTrZu0gW
        Content-Disposition: form-data; name=”action”
    
        submit
        -----WebKitFormBoundary7MA4YWxkTrZu0gW--
    

    On the webservice side, it's consumed in @Consumes("multipart/form-data") form.

    Beware, when testing your webservice using chrome postman, you need to check the form data option(radio button) and File menu from the dropdown box to send attachment. Explicit provision of content-type as multipart/form-data throws an error. Because boundary is missing as it overrides the curl request of post man to server with content-type by appending the boundary which works fine.

    See RFC1341 sec7.2 The Multipart Content-Type

提交回复
热议问题