邮递员请求返回201,OkHttp3请求返回400

时间:2018-02-04 15:20:24

标签: android api post okhttp3

我需要向API发送数据,我使用的是OkHttp 3.9.1

OkHttpClient client = new OkHttpClient();
String path = "/api/mobileinspector";
Request.Builder builder = new Request.Builder();
URI url = URI.create(ApiManager.apiURL + path);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.addFormDataPart("MobileInspector[fio]", fio);
builder.addFormDataPart("MobileInspector[email]", email);
builder.addFormDataPart("MobileInspector[phone]", phone);
builder.addFormDataPart("MobileInspector[description]", description);
builder.addFormDataPart("MobileInspector[address]", address);
builder.addFormDataPart("MobileInspector[category]", category.toString());
MultipartBody body = builder.build();
builder.addHeader("Accept", "application/json")
    .addHeader("Content-Type", "multipart/form-data")
    .url(url.toURL())
    .post(body);
response = client.newCall(builder.build()).execute();

API返回400,这意味着API无法识别数据。 我在Postiman尝试了相同的请求,并且工作正常,按预期返回201.

我使用wireshark从邮递员和模拟器中捕获了http请求。我无法在这些请求中发现任何差异:

OkHttp提出了这个要求:

  

8G ^ |电子@@`} P〜R·]' Z @

     

m'< 8POST / api / mobileinspector HTTP / 1.1接受:application / json   内容类型:multipart / mixed;   boundary = 28ede98e-87b6-4701-b587-57bbcdccb802内容长度:842   主机:API_CORRECT_IP连接:保持活动接受编码:gzip   用户代理:okhttp / 3.9.1

     

- 28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;名称=" MobileInspector [FIO]"内容长度:3

     

123   --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;命名=" MobileInspector [电子邮件]"内容长度:11

     

123@eqw.sda   --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;命名=" MobileInspector [电话]"内容长度:7

     

7232321   --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;命名=" MobileInspector [描述]"内容长度:5

     

21321   --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;命名=" MobileInspector [地址]"内容长度:7

     

2332121   --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition:form-data;命名=" MobileInspector [类别]"内容长度:1

     

2   --28ede98e-87b6-4701-b587-57bbcdccb802 -

邮差是这样做的:

  

8G ^ |电子@@`} PA8 @

     

Y'< 3POST / api / mobileinspector HTTP / 1.1缓存控制:无缓存   Postman-Token:8e425452-a60d-4209-8868-acf9ebc9986b User-Agent:   PostmanRuntime / 7.1.1接受: / 主机:API_CORRECT_IP接受编码:   gzip,deflate content-type:multipart / form-data;   边界= -------------------------- 067684848634261464344219   content-length:841连接:keep-alive

     

---------------------------- 067684848634261464344219 Content-Disposition:form-data;名称=" MobileInspector [FIO]"

     

测试   ---------------------------- 067684848634261464344219 Content-Disposition:form-data;命名=" MobileInspector [电子邮件]"

     

test@example.com   ---------------------------- 067684848634261464344219 Content-Disposition:form-data;命名=" MobileInspector [地址]"

     

地址   ---------------------------- 067684848634261464344219 Content-Disposition:form-data;名称=" MobileInspector [电话]"

     

12312312   ---------------------------- 067684848634261464344219 Content-Disposition:form-data;命名=" MobileInspector [类别]"

     

1   ---------------------------- 067684848634261464344219 Content-Disposition:form-data;名称=" MobileInspector [描述]"

     

没有描述   ---------------------------- 067684848634261464344219 -

1 个答案:

答案 0 :(得分:1)

这两部分差别很大:

okhttp:

  

m'< 8POST / api / mobileinspector HTTP / 1.1接受:application / json Content-Type:multipart / mixed; boundary = 28ede98e-87b6-4701-b587-57bbcdccb802 Content-Length:842主机:API_CORRECT_IP连接:Keep-Alive Accept-Encoding:gzip User-Agent:okhttp / 3.9.1

邮差:

  

Y'< 3POST / api / mobileinspector HTTP / 1.1 cache-control:no-cache Postman-Token:8e425452-a60d-4209-8868-acf9ebc9986b User-Agent:PostmanRuntime / 7.1.1 Accept:/ Host :API_CORRECT_IP accept-encoding:gzip,deflate content-type:multipart / form-data; boundary = -------------------------- 067684848634261464344219 content-length:841 Connection:keep-alive

邮差发送内容为multipart/form-data,而okhttp设置multipart/mixed。这个问题几乎肯定与API意外的内容类型有关。

尝试将构建器设置为:

RequestBody requestBody = new MultipartBody.Builder()
      .type(MultipartBody.FORM)
      // ...
      .build();