Skip to main content

(JAX-RS) How to correctly recieve multipart message using @Post ?

5 replies [Last post]
ivan_g_s
Offline
Joined: 2008-08-23

Hello!

I just can't find the right solution:
I want to send data to a RESTlet using POST request.
How to declare the RESTlet's method header, which annotations are needed?
I want to send multiple parts using one request, so I use a multipart entity from apache-commons-http-client package.

Client contains following code:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(new URI(address));
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("part1", new StringBody("value1"));
multipartEntity.addPart("part2", new StringBody("value2"));
httpPost.setEntity(new MultipartEntity());

How to annotate the RESTlet?

@Path("/test")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response Test(String in1,String in2){ ... }

or is it not just "String in1" but "@FormParam String in1" ?
or is it "MediaType.APPLICATION_FORM_URLENCODED" ?
or is it not String but byte[]? Or InputStream?

I tried every possible combination.
Either I get wrong HTTP error 415 (unsupported media type),
or I get plain representation of all the multipart data inside the first string
(wich all those --BbC04yblablabla).

I'm using integrated Jersey and JAX-RS implementations of the latest Netbeans build.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Paul Sandoz

On Nov 25, 2008, at 2:43 PM, glassfish@javadesktop.org wrote:

> Hello!
>
> I just can't find the right solution:
> I want to send data to a RESTlet using POST request.
> How to declare the RESTlet's method header, which annotations are
> needed?
> I want to send multiple parts using one request, so I use a
> multipart entity from apache-commons-http-client package.
>
> Client contains following code:
>
> HttpClient httpClient = new DefaultHttpClient();
> HttpPost httpPost = new HttpPost(new URI(address));
> MultipartEntity multipartEntity = new MultipartEntity();
> multipartEntity.addPart("part1", new StringBody("value1"));
> multipartEntity.addPart("part2", new StringBody("value2"));
> httpPost.setEntity(new MultipartEntity());
>

What is the Content-Type that the Apache HTTP client sends for the
POST request?

> How to annotate the RESTlet?
>
> @Path("/test")
> @POST
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public Response Test(String in1,String in2){ ... }
>
> or is it not just "String in1" but "@FormParam String in1" ?

public Response Test(@FormParam("part1") String p1,
@FormParam("part2") String p1)

Or you can use JavaMail if you need to get access to the actual
multipart message and/or you are dealing with "multipart/related" or
"multipart/mixed". For example:

@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response Test(javax.mail.internet.MimeMultipart multiPart)

Or:

@Consumes(""multipart/*")
public Response Test(javax.mail.internet.MimeMultipart multiPart)

>
> or is it "MediaType.APPLICATION_FORM_URLENCODED" ?

No, since you are using MIME multipart on the client side the Content-
Type of the request should should be "multipart/form-data". Since you
are getting a 415 response back from the server i am guessing the
client is not setting the Content-Type to "multipart/form-data".

>
> or is it not String but byte[]? Or InputStream?
>
> I tried every possible combination.
> Either I get wrong HTTP error 415 (unsupported media type),
> or I get plain representation of all the multipart data inside the
> first string
> (wich all those --BbC04yblablabla).
>

I presume for the latter case it is when the @Consumes something other
than @Consumes(MediaType.MULTIPART_FORM_DATA).

Paul.

> I'm using integrated Jersey and JAX-RS implementations of the latest
> Netbeans build.
> [Message sent by forum member 'ivan_g_s' (ivan_g_s)]
>
> http://forums.java.net/jive/thread.jspa?messageID=318634
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

ivan_g_s
Offline
Joined: 2008-08-23

Thank you for your reply.

Sadly, it doesn't work with @FormParam("part1") String p1 ...
I tried before, the result is: both strings are null.
Apache is sending: multipart/form-data; boundary=Tl14abid [...]

I have no java mail lib and I don't want additional dependency in my project just to be able to read multipart data. Is there really no other way?

Paul Sandoz

On Nov 25, 2008, at 6:28 PM, glassfish@javadesktop.org wrote:

> Thank you for your reply.
>
> Sadly, it doesn't work with @FormParam("part1") String p1 ...
> I tried before, the result is: both strings are null.
> Apache is sending: multipart/form-data; boundary=Tl14abid [...]
>
> I have no java mail lib and I don't want additional dependency in my
> project just to be able to read multipart data. Is there really no
> other way?

Unfortunately not. What are your objections to the additional
dependency on JavaMail?

The only way you will get support for processing multipart messages is
if you add the JavaMail jar file to the classpath of your web app [1].
If you are using an app server like GF then it will already be included.

The alternative is to not use multipart/* at all and instead use
"application/x-www-urlencoded" which is supported without any
additional dependencies. This approach is very useful when you are
sending small character-based content. If you want to send larger
character-based content or binary content then multipart/* is
recommended.

Paul.

[1] https://jersey.dev.java.net/source/browse/*checkout*/jersey/trunk/jersey...

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

ivan_g_s
Offline
Joined: 2008-08-23

Hmm, javamail is in glassfish you say? I use glassfish (included in latest netbeans build) and don't have it.
Ok I'll probable go with javamail, as you suggested.

Unless there is are more intuitive (or more elegant) way to handle post requests using normal servlets...

Paul Sandoz

On Dec 2, 2008, at 3:44 PM, glassfish@javadesktop.org wrote:

> Hmm, javamail is in glassfish you say? I use glassfish (included in
> latest netbeans build) and don't have it.

It should be there:

- for v2 see lib/mail.jar

- for v3 see glassfish/modules/javax.mail.jar

If you create a new Web application, ensure you are using Java EE 5,
then you should be able to reference and use the JavaMail classes.

>
> Ok I'll probable go with javamail, as you suggested.
>
> Unless there is are more intuitive (or more elegant) way to handle
> post requests using normal servlets...

For multipart/form-data i do not know of anything.

The use of @FormParam with multipart/form-data is reasonably elegant
IMHO as it hides much of the processing details, but i think we can
improve it by also enabling a Java bean approach.

Paul.

>
> [Message sent by forum member 'ivan_g_s' (ivan_g_s)]
>
> http://forums.java.net/jive/thread.jspa?messageID=319558
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net