简体   繁体   English

为什么我的多部分表单数据会转换为JSON?

[英]Why does my multipart form-data get converted to JSON?

I need to do a POST towards an API where multipart form-data is required with one record where the key 'json' and the value is a string containing a json object. 我需要对需要多部分表单数据的API进行POST,其中一条记录的键为“ json”,值是一个包含json对象的字符串。

I'm trying to do it using AndroidAnnotations like this: 我正在尝试使用AndroidAnnotations这样来做到这一点:

@Post(API_CREATE_PATH + API_KEY)
CreateResponse create(Map<String,String> map);

A valid request looks like the following, done with Postman: 使用Postman完成的有效请求如下所示:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="json"

{"test":"data"}
----WebKitFormBoundary7MA4YWxkTrZu0gW

Instead of that, the actual request body contains "json":"{"test":"data"}" , and I can't understand why. 取而代之的是,实际的请求正文包含"json":"{"test":"data"}" ,我不明白为什么。

You can send a multi-part POST in this way, as explained in the wiki . 您可以通过这种方式发送多部分的POST,如Wiki中所述

@Rest(rootUrl = "http://company.com/ajax/services", converters = FormHttpMessageConverter.class)
public interface MyRestClient extends RestClientHeaders {

  @Post(API_CREATE_PATH + API_KEY)
  @RequiresHeader(HttpHeaders.CONTENT_TYPE)
  CreateResponse create(MultiValueMap<String,Object> map);
}

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.set("test","data");

client.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE);

client.create(map);

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

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