2

I am creating a restful web service that processes multiple files from users. from google it seems that the correct mime type should be multipart/mixed so my java web service code (based on Jersey) is something like:

@POST
@Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
@Produces({
    MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
})
@Path("/{Id}")
public CourseBean updateCourse(@PathParam("Id") final String id, final MultiPart multipart)
    throws WebServiceException
{
    //operations on multipart
    String this.id = id;
    return null;
}

in the browser, I run below html to try to upload files to the web service:

<h1></h1>

<p>files</p>
<FORM action="http://localhost:8080/rest/1"
       enctype="multipart/mixed"
       method="POST">
<p>
   name:<INPUT type="text" name="submit-name"><BR>
   file <INPUT type="file" name="file"><BR>
   attachment <INPUT type="file" name="attachment"><BR>
   <INPUT type="submit" value="Send"> <INPUT type="reset">
 </FORM>

</body>
</html>

please note that the enctype here is "multipart/mixed". however after I select files and click the send button, the mime type of the http request my web service gets is changed to application/x-www-form-urlencoded, and this causes a "Unsupported Media Type" error in the web service side.

but if I change the enctype in the html to multipart/form-data, the mime type of the received request is the same: multipart/form-data.

so my question is, how can I create a html form that can send a http message with mime type "multipart/mixed"? with this html, I can test my web service.

thank you very much.

danny
  • 3,046
  • 3
  • 23
  • 28

3 Answers3

2

HTML forms do not support sending data as multipart/mixed. The closest they come, and what you probably want to use is multipart/form-data. If your REST webservice can only accept multipart/mixed, then you won't be able to call it directly from an HTML form.

See this question for more details: What does enctype='multipart/form-data' mean?

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
  • thanks for letting me know html form does not support multipart/mixed. is that possible to use javascript to change the form's mime type to multipart/mixed when submit the form? – danny Jun 11 '13 at 04:02
  • No, it's not possible. A web browser doesn't know anything about `multipart/mixed`, so a form's data can't be submitted in that format. – jcsanyi Jun 11 '13 at 05:22
0

HTML forms always use multipart/form-data as the encoding. This is a way to send multiple blocks of data in one stream. Each block has its own header (this is the "multipart" feature). A web page will always send one such block for each "successful" element in the form, which means in practice all elements with content, and the submit button that was clicked.

According to RFC1867, which defines <input type="file">, files can be sent in two ways: when a single file is sent, it is itself a part in the stream. And when multiple files are sent, they are packed as a multipart/mixed block within the multipart/form-data: the POST request still has the type multipart/form-data, but inside it one of the parts is of type multipart/mixed.

Note that this is only supposed to happen if multiple files are sent from a single input element. If there are several input elements, they will each be sent as their own block and no nested multipart stream will be created.

That RFC was written in 1995, so you'd expect HTML to have a way to send multiple files. That took a long time, but it has been added as part of HTML5 with the multiple attribute.

However, the current browser implementations don't use the method described in the RFC. Instead, they will send multiple fields with the same name. In other words, browsers never generate a multipart/mixed field.

Bas Wijnen
  • 1,288
  • 1
  • 8
  • 17
  • This is not true; the 'name' key-value in the body-header, Content-Disposition, will be the same for all those files; but it will not send multipart/mixed. – Henrik Apr 07 '15 at 08:40
  • @Henrik I hadn't checked this and just assumed they would follow the RFC. I've updated the answer now. Thanks for notifying. – Bas Wijnen Apr 08 '15 at 14:53
  • So did I, until I wrote a http client ;) – Henrik Apr 08 '15 at 18:13
  • I've written a server, but I didn't get to multipart/mixed yet. This saves me some work. :-) – Bas Wijnen Apr 09 '15 at 21:05
0

This is what a request with a multi-file browse control looks like:

Sample-form.html:

  <form action="http://localhost:1234" method="post" enctype="multipart/form-data">
    <p><input type="text" name="message" value="Hello World">
    <p><input type="file" name="images" multiple>
    <p><button type="submit">Submit</button>
  </form>

This is what the server gets from Safari:

POST / HTTP/1.1

Host:

POST / HTTP/1.1
Host: localhost:1234
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryN8Y1MQiNiQs7fEEd
Origin: file://
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.4.10 (KHTML, like Gecko) Version/7.1.4 Safari/537.85.13
Content-Length: 527
Accept-Language: en-us
Accept-Encoding: gzip, deflate

------WebKitFormBoundaryN8Y1MQiNiQs7fEEd
Content-Disposition: form-data; name="message"

Hello World
------WebKitFormBoundaryN8Y1MQiNiQs7fEEd
Content-Disposition: form-data; name="images"; filename="testfile1.txt"
Content-Type: text/plain

Hello World, no trailling newlines
------WebKitFormBoundaryN8Y1MQiNiQs7fEEd
Content-Disposition: form-data; name="images"; filename="testfile2.txt"
Content-Type: text/plain

Hello world, with TWO newlines at the end (NOT CRLF)


------WebKitFormBoundaryN8Y1MQiNiQs7fEEd--

From Firefox:

POST / HTTP/1.1
Host: localhost:1234
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------16701619336528579941090352635
Content-Length: 599

-----------------------------16701619336528579941090352635
Content-Disposition: form-data; name="message"

Hello World
-----------------------------16701619336528579941090352635
Content-Disposition: form-data; name="images"; filename="testfile1.txt"
Content-Type: text/plain

Hello World, no trailling newlines
-----------------------------16701619336528579941090352635
Content-Disposition: form-data; name="images"; filename="testfile2.txt"
Content-Type: text/plain

Hello world, with TWO newlines at the end (NOT CRLF)


-----------------------------16701619336528579941090352635--
Henrik
  • 9,714
  • 5
  • 53
  • 87