REST - 带有 JSON 的 HTTP Post Multipart [英] REST - HTTP Post Multipart with JSON

查看:37
本文介绍了REST - 带有 JSON 的 HTTP Post Multipart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要接收一个仅包含 2 个参数的 HTTP Post Multipart:

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

  • 一个 JSON 字符串
  • 一个二进制文件

身体的正确设置方法是什么?我将使用 Chrome REST 控制台测试 HTTP 调用,所以我想知道正确的解决方案是否是为 JSON 参数和二进制文件设置标签"键.

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.

在服务器端,我使用的是 Resteasy 2.x,我将像这样读取 Multipart 正文:

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
}

这是要走的路吗?使用标识该特定内容处置的键myJsonName"检索我的 JSON 字符串是否正确?有没有其他方法可以在一个 HTTP 多部分请求中接收这 2 个内容?

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?

提前致谢

推荐答案

如果我理解正确,您想从 HTTP/REST 控制台手动编写多部分请求.多部分格式很简单;在 HTML 4.01 规范中可以找到简要介绍.你需要想出一个边界,它是一个在内容中找不到的字符串,比如说 HereGoes.你设置请求头 Content-Type: multipart/form-data;边界=HereGoes.那么这应该是一个有效的请求正文:

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

这篇关于REST - 带有 JSON 的 HTTP Post Multipart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆