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

问题描述

我需要收到一个HTTP Post Multipart,它只包含2个参数:

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控制台手动编写多部分请求。多部分格式很简单;可以在中找到简要介绍。你需要提出一个边界,这是一个在内容中找不到的字符串,假设 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:48