What's the difference between 'multipart/r

2020-05-27 09:25发布

问题:

I was able to find a lot of information about multipart/form-data but not much about multipart/related. In terms of the protocol / request format, can someone explain the differences between these two http specifications when it comes to file uploading?

回答1:

multipart/form-data is used to upload files of MIME-compatible representation, such as pictures and video files, and related metadata a single POST request. That's what happens when you fill in a form online with attached pictures and then press the "Submit" button.

multipart/related is used for compound documents and you would need to combine the separate body parts to provide the full meaning of the message. One use case would be submitting some Base64-encoded images together with the associated metadata.

One POST request sample is (https://cloud.google.com/storage/docs/json_api/v1/how-tos/multipart-upload):

POST https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=multipart HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: [NUMBER_OF_BYTES_IN_ENTIRE_REQUEST_BODY]

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{
  "name": "myObject"
}

--foo_bar_baz
Content-Type: image/jpeg

[JPEG_DATA]
--foo_bar_baz--

You can find more details at https://msdn.microsoft.com/en-us/library/ms527355(v=exchg.10).aspx