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=???
.
Is the ???
free to be defined by the user? Or it is generally from the HTML? Is it possible for me to define the ??? = abcdefg
?
Is the ???
free to be defined by the user?
Yes.
or is it supplied by the HTML?
No. HTML has nothing to do with that. Read below.
Is it possible for me to define the ???
as abcdefg
?
Yes.
If you want to send the following data to the web server:
name = John
age = 12
using application/x-www-form-urlencoded
would be like this:
name=John&age=12
As you can see, the server knows that parameters are separated by an ampersand &
. If &
is required for a parameter value then it must be encoded.
So how does the server know where a parameter value starts and ends when it receives an HTTP request using multipart/form-data
?
Using the boundary, similar to &
.
For example:
--XXX
Content-Disposition: form-data; name=\"name\"
John
--XXX
Content-Disposition: form-data; name=\"age\"
12
--XXX--
In that case, the boundary value is XXX
. You specify it in the Content-Type
header so that the server knows how to split the data it receives.
So you need to:
The exact answer to the question is: yes, you can use an arbitrary value for the boundary
parameter, given it does not exceed 70 bytes in length and consists only of 7-bit US-ASCII
(printable) characters.
If you are using one of multipart/*
content types, you are actually required to specify the boundary
parameter in the Content-Type
header, otherwise the server (in the case of an HTTP request) will not be able to parse the payload.
You probably also want to set the charset
parameter to UTF-8
in your Content-Type
header, unless you can be absolutely sure that only US-ASCII
charset will be used in the payload data.
A few relevant excerpts from the RFC2046:
Here is an example using an arbitrary boundary:
Content-Type: multipart/form-data; charset=utf-8; boundary=\"another cool boundary\"
--another cool boundary
Content-Disposition: form-data; name=\"foo\"
bar
--another cool boundary
Content-Disposition: form-data; name=\"baz\"
quux
--another cool boundary--
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