Is it possible to have a nested MultipartEntities or FormBodyPart in a multipart POST?
Asked Answered
Q

5

13

I'm trying to make something the following server POST request using MultipartEntity:

parameters: {"parameter1"=>"parameter1", "parameter2"=>{"sub_parameter1"=>"sub_parameter1", "sub_parameter2"=>"sub_parameter2"}}

I am currently using something like:

multipartEntity.addPart("parameter1", new StringBody("parameter1"));

FormBodyPart parameter2 = new FormBodyPart("parameter2", new StringBody("")); // It wouldn't allow a null ContentBody

parameter2.addField("sub_parameter1", "sub_parameter1");
parameter2.addField("sub_parameter2", "sub_parameter2");

However, the sub fields do not carry though. I just get:

parameters: {"parameter1"=>"parameter1", "parameter2"=>""}

How do I create a nested structure in either the MultipartEntity or the FormBodyPart elements it contains?

Qulllon answered 28/4, 2013 at 5:39 Comment(2)
did you end up finding a solution to this? I'm having the same problem.Ostracoderm
i am looking for something like this too but couldnot get it workingInstitutive
D
8

once you've seen how the form entries are transferred over HTTP connection, you'll understand it's impossible to have anything nested with the multiform request as well as with the url-encoded request.

things are very simple. multipart form request has the format of:

--- content-boundary ---
Content-Disposition: form-data; name="form_data_name"

[content (skipped)]
--- content-boundary ---

that's it. there's stream of single data form entries in the format: [form entry name] -> [form entry content] that repeats for every entry in the form. it's not recursive, therefore there may not be any nested structures.

Sources:

  1. 17.13.4 Form content types
  2. RFC 2045 Internet Message Bodies
Discord answered 19/5, 2014 at 17:46 Comment(1)
It is a really nice solution and that, pretty much sums up that 'nested' is not a word for Multipart Entity. Thanks!Amadou
L
2

Consider sending a json in the body of the request. That way you can send whatever you want.

Larkins answered 19/5, 2014 at 17:37 Comment(0)
L
2

Something like this:

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("selectedGroup", new StringBody(group));
reqEntity.addPart("selectedService", new StringBody(service.toString()));
reqEntity.addPart("selectedTransformation", new StringBody(transformation.toString()));
reqEntity.addPart("projectId", new StringBody(projectId.toString()));

Check exapmle 8 of this link. Also exapmple 23 Example Site

Hope this helps you with your problem.

Lasonde answered 20/5, 2014 at 7:8 Comment(0)
J
1

why dont you post whole Json object instead of post each string value of Json object.

Go through this link http://hmkcode.com/android-send-json-data-to-server/

Jupon answered 21/5, 2014 at 5:43 Comment(0)
C
0

I had a similar requirement and I've ended up adjusting the code on my server side to remove parameter 2, in order to use MultipartEntity on the client side. It worked perfectly fine.

parameters: {"parameter1"=>"parameter1", "parameter2"=>{"sub_parameter1"=>"sub_parameter1", "sub_parameter2"=>"sub_parameter2"}}

parameters: {"parameter1"=>"parameter1", "sub_parameter1"=>"sub_parameter1", "sub_parameter2"=>"sub_parameter2"}
Chicoine answered 11/11, 2015 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.