REST - multithreaded HTTP post with JSON

I need to get an HTTP Post Multipart that contains only 2 parameters:

  • Json string
  • Binary file

What is the right way to establish a body? I am going to test an HTTP call using the Chrome REST console, so I wonder if the correct solution is to set a β€œlabel” for the JSON parameter and the binary.

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

@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 that the way? Is it right to get my JSON string using the "myJsonName" key, which identifies the specific content? Is there any other way to get these 2 content in one HTTP request?

Thank you in advance

+76
java json rest resteasy
Jan 31 '12 at 14:20
source share
1 answer

If you understand correctly, you want to compose a multidisciplinary request manually from the HTTP / REST console. The multi-page format is simple; A brief introduction can be found in the HTML 4.01 specification . You need to come up with a border, which is a string not found in the content, say HereGoes . You set the request header Content-Type: multipart/form-data; boundary=HereGoes Content-Type: multipart/form-data; boundary=HereGoes . Then it must be a valid request object:

 --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-- 
+127
Jan 31 '12 at 15:34
source



All Articles