You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "vlad.balan" <vl...@gmail.com> on 2018/06/08 16:43:01 UTC

JAX-RS how to embed multipart/mixed in a multipart/form-data?

Hello

and thanks whoevers reads the question.


In the CXF doc here

http://cxf.apache.org/docs/jax-rs-multiparts.html
<http://cxf.apache.org/docs/jax-rs-multiparts.html>  

it says:

"

Finally, multipart/form-data requests with multiple files (file uploads) can
be supported too. For example, this request can be handled by a method with
the signature like :

@POST
@Path("/books/filesform")
@Produces("text/xml")
@Consumes("multipart/form-data")
public Response addBookFilesForm(@Multipart("owner") String name,
                                 @Multipart("files") List<Book> books) {} 

"



But how to generate that request from the java client side?

This is the request content:


--bqJky99mlBWa-ZuqjC53mG6EzbmlxB
Content-Disposition: form-data; name="owner"
Content-Type: text/plain

Larry
--bqJky99mlBWa-ZuqjC53mG6EzbmlxB
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=_Part_4_701508.1145579811786

--_Part_4_701508.1145579811786
Content-Disposition: form-data; name="book1"
Content-Type: application/json; charset=US-ASCII
Content-Transfer-Encoding: 8bit

{"Book":{"name":"CXF in Action - 1","id":123}}
--_Part_4_701508.1145579811786
Content-Disposition: form-data; name="book2"
Content-Type: application/json; charset=US-ASCII
Content-Transfer-Encoding: 8bit

{"Book":{"name":"CXF in Action - 2%","id":124}}
--_Part_4_701508.1145579811786--
--bqJky99mlBWa-ZuqjC53mG6EzbmlxB--


Thanks



--
Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html

Re: JAX-RS how to embed multipart/mixed in a multipart/form-data?

Posted by Andy McCright <j....@gmail.com>.
Hi Vlad,

I admit that this isn't my area of expertise, but could you send the code
you are using to send the general list and the generated request output?

Thanks,
Andy

On Tue, Jun 19, 2018 at 4:08 AM vlad.balan <vl...@gmail.com> wrote:

> Hello and thanks for the reply.
>
> In fact, the ideea is
> - to work with beans.
> - how would you do in java to nest those 2 beans under a multipart/mixed?
>
>
> I tried something like
>
>
>                 // ------------ THE SUB LIST
>
>                 List<Attachment> listSecondMultipart = new
> ArrayList<Attachment>();
>
>                 listSecondMultipart.add(new Attachment("beta",
> "application/json", new
> SomeBean("beta")));
>                 listSecondMultipart.add(new Attachment("gamma",
> "application/json", new
> SomeBean("gamma")));
>
>
>                 // -------------- THE GENERAL LIST
>
>                 List<Attachment> list = new ArrayList<Attachment>();
>
>                 list.add(new Attachment("alfa", "application/json", new
> SomeBean("alfa")));
>                 list.add(new Attachment("delta", "multipart/mixed",
> listSecondMultipart));
>
>
> But the generated form-data is a chaos.
>
> So it doesn't serialize as i expect.
>
>
> Thanks.
>
>
>
> --
> Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html
>

Re: JAX-RS how to embed multipart/mixed in a multipart/form-data?

Posted by "vlad.balan" <vl...@gmail.com>.
Hello and thanks for the reply.

In fact, the ideea is 
- to work with beans.
- how would you do in java to nest those 2 beans under a multipart/mixed?


I tried something like


                // ------------ THE SUB LIST

		List<Attachment> listSecondMultipart = new ArrayList<Attachment>();

		listSecondMultipart.add(new Attachment("beta", "application/json", new
SomeBean("beta")));
		listSecondMultipart.add(new Attachment("gamma", "application/json", new
SomeBean("gamma")));


                // -------------- THE GENERAL LIST

		List<Attachment> list = new ArrayList<Attachment>();
		
		list.add(new Attachment("alfa", "application/json", new
SomeBean("alfa")));
                list.add(new Attachment("delta", "multipart/mixed",
listSecondMultipart));


But the generated form-data is a chaos.

So it doesn't serialize as i expect.


Thanks.



--
Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html

Re: JAX-RS how to embed multipart/mixed in a multipart/form-data?

Posted by Andy McCright <j....@gmail.com>.
Hi Vlad,

I'm not 100% positive, but I think you can use the Multipart/Attachment
APIs on the client to send the request.  Here is an example of the
Multipart APIs:
https://github.com/apache/cxf/blob/master/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/multipart/MultipartBodyTest.java

So, if I understand it correctly, then you should be able able to send the
request like this:

List<Attachment> atts = new ArrayList<>();

atts.add(createAttachment("owners", pathToOwnersFile));

atts.add(createAttachment("files", pathToBooksFile)));

MultipartBody b = new MultipartBody(atts);

Client client = ClientBuilder.newClient();

Response r = client.target(myURL).request().post(b);


Hope this helps,

Andy

On Fri, Jun 8, 2018 at 11:43 AM vlad.balan <vl...@gmail.com> wrote:

> Hello
>
> and thanks whoevers reads the question.
>
>
> In the CXF doc here
>
> http://cxf.apache.org/docs/jax-rs-multiparts.html
> <http://cxf.apache.org/docs/jax-rs-multiparts.html>
>
> it says:
>
> "
>
> Finally, multipart/form-data requests with multiple files (file uploads)
> can
> be supported too. For example, this request can be handled by a method with
> the signature like :
>
> @POST
> @Path("/books/filesform")
> @Produces("text/xml")
> @Consumes("multipart/form-data")
> public Response addBookFilesForm(@Multipart("owner") String name,
>                                  @Multipart("files") List<Book> books) {}
>
> "
>
>
>
> But how to generate that request from the java client side?
>
> This is the request content:
>
>
> --bqJky99mlBWa-ZuqjC53mG6EzbmlxB
> Content-Disposition: form-data; name="owner"
> Content-Type: text/plain
>
> Larry
> --bqJky99mlBWa-ZuqjC53mG6EzbmlxB
> Content-Disposition: form-data; name="files"
> Content-Type: multipart/mixed; boundary=_Part_4_701508.1145579811786
>
> --_Part_4_701508.1145579811786
> Content-Disposition: form-data; name="book1"
> Content-Type: application/json; charset=US-ASCII
> Content-Transfer-Encoding: 8bit
>
> {"Book":{"name":"CXF in Action - 1","id":123}}
> --_Part_4_701508.1145579811786
> Content-Disposition: form-data; name="book2"
> Content-Type: application/json; charset=US-ASCII
> Content-Transfer-Encoding: 8bit
>
> {"Book":{"name":"CXF in Action - 2%","id":124}}
> --_Part_4_701508.1145579811786--
> --bqJky99mlBWa-ZuqjC53mG6EzbmlxB--
>
>
> Thanks
>
>
>
> --
> Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html
>