Re: [Resteasy-users] Client for multipart/form-data?
Still trying to get multipart/form-data upload to work with a Resteasy client ... After some experimenting, I now found out that you can add an @PartType annotation to the client's Map parameter, like this: @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @Path("wherever") Object doUpload(@PartType(MediaType.APPLICATION_OCTET_STREAM) Map parts); And then use this as: Map parts = new HashMap(); parts.put("file", new FileDataSource(new File("mypic.jpg"))); Object response = client.doUpload(parts); Resteasy will then use org.jboss.resteasy.plugins.providers.multipart.MapMultipartFormDataWriter. That way, however, two important things are missing from the HTTP request (compared to a direct upload from a browser): the Content-Type is hard-wired to application/octet-stream because of the annotation and the mime part containing the file data does not have a Content-Disposition header with a "filename" parameter with the DataSource's name (in this example, this should be Content-Disposition: attachment; filename="mypic.jpg"). Might this get fixed or do you suggest coding the upload with a hand-crafted request? Jay > Hi, > > how do I parameterize an interface method for a POST call that > consumes a multipart/form-data, for use with use > org.jboss.resteasy.client.ProxyFactory? > > The server implements something like this: > > @POST > @Consumes(MediaType.MULTIPART_FORM_DATA) > @Produces(MediaType.APPLICATION_JSON) > @Path("wherever") > public Response doFooImpl(MultipartFormDataInput object) { > List items = object.getFormDataMap().get("ITEM"); /* ... */ > } > > In the client, I tried this: > > @POST > @Consumes(MediaType.MULTIPART_FORM_DATA) > @Produces(MediaType.APPLICATION_JSON) > @Path("wherever") > Object doFoo(Map parts); > > But when I run this, the client throws a > > java.lang.RuntimeException: could not find writer for content-type > multipart/form-data type: java.util.Map > > Would it be better to use a javax.mail.Multipart as a client-side > parameter? Or > org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInputImpl > (if that is an "official" API class)? > > Thanks, Jay > -- All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 ___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users
Re: [Resteasy-users] Client for multipart/form-data?
People were having trouble with DataSource. I don't remember if the problem was fixed. Instead of Datasource, try using InputStream or some other media type for the part. Also, there is a @org.jboss.resteasy.annotations.providers.multipart.PartType annotation that is used to specify the Content-Type of the marshalled Multipart map, I don't have an annotation to specify the content-disposition. On 7/6/11 6:49 AM, ujay68 wrote: > Still trying to get multipart/form-data upload to work with a Resteasy > client ... > > After some experimenting, I now found out that you can add an > @PartType annotation to the client's Map parameter, like this: > > @POST > @Consumes(MediaType.MULTIPART_FORM_DATA) > @Produces(MediaType.APPLICATION_JSON) > @Path("wherever") > Object doUpload(@PartType(MediaType.APPLICATION_OCTET_STREAM) > Map parts); > > And then use this as: > > Map parts = new HashMap(); > parts.put("file", new FileDataSource(new File("mypic.jpg"))); > Object response = client.doUpload(parts); > > Resteasy will then use > org.jboss.resteasy.plugins.providers.multipart.MapMultipartFormDataWriter. > > That way, however, two important things are missing from the HTTP > request (compared to a direct upload from a browser): the Content-Type > is hard-wired to application/octet-stream because of the annotation > and the mime part containing the file data does not have a > Content-Disposition header with a "filename" parameter with the > DataSource's name (in this example, this should be > Content-Disposition: attachment; filename="mypic.jpg"). > > Might this get fixed or do you suggest coding the upload with a > hand-crafted request? > > Jay > > >> Hi, >> >> how do I parameterize an interface method for a POST call that >> consumes a multipart/form-data, for use with use >> org.jboss.resteasy.client.ProxyFactory? >> >> The server implements something like this: >> >> @POST >> @Consumes(MediaType.MULTIPART_FORM_DATA) >> @Produces(MediaType.APPLICATION_JSON) >> @Path("wherever") >> public Response doFooImpl(MultipartFormDataInput object) { >> List items = object.getFormDataMap().get("ITEM"); /* ... */ >> } >> >> In the client, I tried this: >> >> @POST >> @Consumes(MediaType.MULTIPART_FORM_DATA) >> @Produces(MediaType.APPLICATION_JSON) >> @Path("wherever") >> Object doFoo(Map parts); >> >> But when I run this, the client throws a >> >> java.lang.RuntimeException: could not find writer for content-type >> multipart/form-data type: java.util.Map >> >> Would it be better to use a javax.mail.Multipart as a client-side >> parameter? Or >> org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInputImpl >> (if that is an "official" API class)? >> >> Thanks, Jay >> > > -- > All of the data generated in your IT infrastructure is seriously valuable. > Why? It contains a definitive record of application performance, security > threats, fraudulent activity, and more. Splunk takes this data and makes > sense of it. IT sense. And common sense. > http://p.sf.net/sfu/splunk-d2d-c2 > ___ > Resteasy-users mailing list > Resteasy-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/resteasy-users -- Bill Burke JBoss, a division of Red Hat http://bill.burkecentral.com -- All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 ___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users
Re: [Resteasy-users] Client for multipart/form-data?
Bill, thanks. > People were having trouble with DataSource. That was probably me, on the server side. ;-) https://issues.jboss.org/browse/RESTEASY-545 > Instead of Datasource, try using InputStream or some > other media type for the part. DataSource is not a problem here if you use, eg, a javax.activation.FileDataSource, as in the code snippet of my previous post, which works fine. > Also, there is a > @org.jboss.resteasy.annotations.providers.multipart.PartType annotation > that is used to specify the Content-Type of the marshalled Multipart > map, I used that annotation to make it work at all, but this annotation is, of course, not dynamic, so if the client wants to upload files of different media types, you cannot change that dynamically. Also, the annotation is for the entire map and not per entry, so if you tried to upload several files in one request, all the files would have to have the same media type. Jay -- All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2 ___ Resteasy-users mailing list Resteasy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/resteasy-users