users@glassfish.java.net

Re: (JAX-RS) How to correctly recieve multipart message using _at_Post ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 25 Nov 2008 14:58:32 +0100

On Nov 25, 2008, at 2:43 PM, glassfish_at_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_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>