简体   繁体   English

REST-使用JSON的HTTP发布多部分

[英]REST - HTTP Post Multipart with JSON

I need to receive an HTTP Post Multipart which contains only 2 parameters: 我需要接收仅包含2个参数的HTTP Post Multipart:

  • A JSON string JSON字符串
  • 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. 我要测试使用Chrome REST控制台HTTP调用,所以我不知道是否正确的解决办法是设置为JSON参数和二进制文件“标签”键。

On the server side I'm using Resteasy 2.x, and I'm going to read the Multipart body like this: 在服务器端,我正在使用Resteasy 2.x,我将像这样阅读Multipart主体:

@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? 使用标识该特定内容处置的键“ myJsonName”检索我的JSON字符串是否正确? Are there any other way to receive these 2 content in one HTTP multipart request? 还有其他方法可以在一个HTTP多部分请求中接收这2个内容吗?

Thanks in advance 提前致谢

If I understand you correctly, you want to compose a multipart request manually from an HTTP/REST console. 如果我对您的理解正确,那么您想从HTTP / REST控制台手动编写一个多部分请求。 The multipart format is simple; 分段格式很简单; a brief introduction can be found in the HTML 4.01 spec . 简短的介绍可以在HTML 4.01规范中找到 You need to come up with a boundary, which is a string not found in the content, let's say HereGoes . 您需要提出一个边界,即在内容中找不到的字符串,比方说HereGoes You set request header Content-Type: multipart/form-data; boundary=HereGoes 您设置请求标头Content-Type: multipart/form-data; boundary=HereGoes Content-Type: multipart/form-data; boundary=HereGoes . 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--

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM