REST - HTTP Post Multipart with JSON

时间:2022-08-24 13:51:08

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

我需要收到一个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参数和二进制文件设置一个“label”键。

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

在服务器端,我使用Resteasy 2。我将会读到多部分的身体,像这样:

@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?

是这样吗?使用标识特定内容配置的关键“myJsonName”来检索我的JSON字符串是否正确?在一个HTTP多部分请求中有其他方法接收这两个内容吗?

Thanks in advance

谢谢提前

1 个解决方案

#1


123  

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:

如果我理解正确,您希望从HTTP/REST控制台手动组合一个多部分请求。多部分格式很简单;在HTML 4.01规范中可以找到一个简短的介绍。您需要找到一个边界,这个边界是内容中没有的字符串,假设是如下所示。设置请求头内容类型:多部分/表单数据;边界= HereGoes。那么这应该是一个有效的请求主体:

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

#1


123  

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:

如果我理解正确,您希望从HTTP/REST控制台手动组合一个多部分请求。多部分格式很简单;在HTML 4.01规范中可以找到一个简短的介绍。您需要找到一个边界,这个边界是内容中没有的字符串,假设是如下所示。设置请求头内容类型:多部分/表单数据;边界= HereGoes。那么这应该是一个有效的请求主体:

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