REST - 使用 JSON 的 HTTP Post 多部分
我需要接收仅包含 2 个参数的 HTTP Post Multipart:
- JSON 字符串
- 二进制文件
设置正文的正确方法是什么? 我将使用 Chrome REST 控制台测试 HTTP 调用,因此我想知道正确的解决方案是否是为 JSON 参数和二进制文件设置“标签”键。
在服务器端,我使用 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
}
这是要走的路吗? 使用标识特定内容处置的键“myJsonName”检索我的 JSON 字符串是否正确? 是否有其他方法可以在一个 HTTP 多部分请求中接收这 2 个内容?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

如果我理解正确的话,您想从 HTTP/REST 控制台手动编写多部分请求。多部分格式很简单;可以在HTML 4.01 规范中找到简要介绍。您需要提出一个边界,它是内容中找不到的字符串,比方说
HereGoes
。您设置请求标头Content-Type: multipart/form-data;边界=这里
。那么这应该是一个有效的请求正文: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 headerContent-Type: multipart/form-data; boundary=HereGoes
. Then this should be a valid request body: