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

牧云@^-^@ 提交于 2021-02-06 15:29:41

问题


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?


回答1:


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



回答2:


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




回答3:


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.




回答4:


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/




回答5:


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"}


来源:https://stackoverflow.com/questions/16259989/is-it-possible-to-have-a-nested-multipartentities-or-formbodypart-in-a-multipart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!