MultipartEntity post array

532 views Asked by At

I'm trying to upload file using http post request. I figured this is doable using MultipartEntity, as such:

MultipartEntity entity = new MultipartEntity();
...
entity.addPart("caption", new StringBody("myCaption"));
entity.addPart("file", new FileBody(file));
entity.addPart("uploadername", new StringBody("myName"));
...

My problem now is the structure by which the post request needs to be made. Instead of the rather straightforward...

{
    "caption":[caption],
    "file":[file to upload],
    "uploadername":[name]
}

..it is..

{
    "newfile":
    {
        "caption":[caption],
        "file":[file to upload]
    },
    "uploadername":[name]
}

If I don't need to post files, I can use JSONObject and put one JSONObject inside the other, but I can't figure out a way to do so for MultipartEntity. Any clues? Thanks in advance.

1

There are 1 answers

0
Christopher Aldo On

..Okay, guess I found a way to do it.

....
entity.addPart("newfile[caption]", new StringBody("myCaption"));
entity.addPart("newfile[file]", new FileBody(file));
entity.addPart("uploadername", new StringBody("myName"));
...