103

I need to receive an HTTP Post Multipart which contains only 2 parameters:

  • A JSON string
  • A binary file

Which is the correct way to set the body? I'm going to test the HTTP call using Chrome REST console, so I'm wondering if the correct solution is to set a "label" key for the JSON parameter and the binary file.

On the server side I'm using Resteasy 2.x, and I'm going to read the Multipart body like this:

@POST
@Consumes("multipart/form-data")
public String postWithPhoto(MultipartFormDataInput  multiPart) {
  Map <String, List<InputPart>> params = multiPart.getFormDataMap();
  String myJson = params.get("myJsonName").get(0).getBodyAsString();
  InputPart imagePart = params.get("photo").get(0);
  //do whatever I need to do with my json and my photo
}

Is this the way to go? Is it correct to retrieve my JSON string using the key "myJsonName" that identify that particular content-disposition? Are there any other way to receive these 2 content in one HTTP multipart request?

2
  • 1
    What kind of REST resource is this? How do two parts relate on the resource level?
    – user647772
    Commented Jan 31, 2012 at 14:21
  • Actually the way we handled this resource is not totally RESTful because the image is a "component" of the resource instead of another resource.
    – thermz
    Commented Jan 31, 2012 at 14:25

1 Answer 1

164

If I understand you correctly, you want to compose a multipart request manually from an HTTP/REST console. The multipart format is simple; a brief introduction can be found in the HTML 4.01 spec. You need to come up with a boundary, which is a string not found in the content, let’s say HereGoes. You set request header Content-Type: multipart/form-data; boundary=HereGoes. Then this should be a valid request body:

--HereGoes
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json

{"foo": "bar"}
--HereGoes
Content-Disposition: form-data; name="photo"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

<...JPEG content in base64...>
--HereGoes--
8
  • You can actually add an attachment in soap-ui too. This alleviates having to pass in the actual post body and the content-type.
    – shane lee
    Commented Mar 28, 2014 at 0:37
  • 1
    Is there a standardized way for coming up with the unique boundary?
    – andig
    Commented Mar 11, 2015 at 17:26
  • 2
    @andig I’m not aware of any. Perhaps you could use a UUID, but that’s not necessarily a good idea. Normally your HTTP library should take care of that for you. Commented Mar 12, 2015 at 20:30
  • 4
    Why use base64 to encode the JPEG data? HTTP allows you to send the raw bytes. Commented Feb 9, 2017 at 17:50
  • 1
    Excellent. With reference to node, following is explained in detail: derpturkey.com/node-multipart-form-data-explained
    – Sourabh
    Commented Apr 9, 2019 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.