开发者

How do boundaries work in multipart post requests?

I trying to upload a file from an iPhone to a server. I'm trying to avoid using any libraries that aren't made by apple, and from what I can tell it looks like I'll need to go pretty low level on constructing my request. Can someone tell me what the "bou开发者_StackOverflowndary" is in a multipart/form-data request and how to use it properly?


The boundary is an arbitrary piece of text which the client uses to delimit the fields of the form being posted. The client declares the boundary it is using as part of the Content-type header.

From the IETF Form-based File Upload in HTML RFC:

A boundary is selected that does not occur in any of the data. (This selection is sometimes done probabilisticly.) Each field of the form is sent, in the order in which it occurs in the form, as a part of the multipart stream. Each part identifies the INPUT name within the original HTML form. Each part should be labelled with an appropriate content-type if the media type is known (e.g., inferred from the file extension or operating system typing information) or as application/octet-stream.

...

6. Examples

Suppose the server supplies the following HTML:

<FORM ACTION="http://server.dom/cgi/handle"
       ENCTYPE="multipart/form-data"
       METHOD=POST>
 What is your name? <INPUT TYPE=TEXT NAME=submitter>
 What files are you sending? <INPUT TYPE=FILE NAME=pics>
 </FORM>

and the user types "Joe Blow" in the name field, and selects a text file "file1.txt" for the answer to 'What files are you sending?'

The client might send back the following data:

   Content-type: multipart/form-data, boundary=AaB03x

   --AaB03x
   content-disposition: form-data; name="field1"

   Joe Blow
   --AaB03x
   content-disposition: form-data; name="pics"; filename="file1.txt"
   Content-Type: text/plain

    ... contents of file1.txt ...
   --AaB03x--

If the user also indicated an image file "file2.gif" for the answer to 'What files are you sending?', the client might client might send back the following data:

   Content-type: multipart/form-data, boundary=AaB03x

   --AaB03x
   content-disposition: form-data; name="field1"

   Joe Blow
   --AaB03x
   content-disposition: form-data; name="pics"
   Content-type: multipart/mixed, boundary=BbC04y

   --BbC04y
   Content-disposition: attachment; filename="file1.txt"

In the first example, the boundary is the fixed string AaB03x. In the second example, the boundary is first AaB03x and then switches to BbC04y.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜