Postman 请求 returns 201,OkHttp3 请求 returns 400
Postman request returns 201, OkHttp3 request returns 400
我需要向 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 returns 400,也就是说API没有识别到数据。
我在 Postiman 中尝试了相同的请求并且它工作正常,按预期返回 201。
我使用 wireshark 捕获了来自 postman 和来自模拟器的 http 请求。我在这些请求中找不到任何差异:
OkHttp 提出了这个请求:
8G^|E@@`}P~r#]'z@
m'<8POST /api/mobileinspector HTTP/1.1 Accept: application/json
Content-Type: multipart/mixed;
boundary=28ede98e-87b6-4701-b587-57bbcdccb802 Content-Length: 842
Host: API_CORRECT_IP Connection: Keep-Alive Accept-Encoding: gzip
User-Agent: okhttp/3.9.1
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[fio]" Content-Length: 3
123
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[email]" Content-Length: 11
123@eqw.sda
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[phone]" Content-Length: 7
7232321
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[description]" Content-Length: 5
21321
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[address]" Content-Length: 7
2332121
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[category]" Content-Length: 1
2
--28ede98e-87b6-4701-b587-57bbcdccb802--
Postman 制作了这个:
8G^|E@@`}Pa8@
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
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[fio]"
test
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[email]"
test@example.com
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[address]"
address
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[phone]"
12312312
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[category]"
1
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[description]"
no description
----------------------------067684848634261464344219--
这两部分差异很大:
还行http:
m'<8POST /api/mobileinspector HTTP/1.1 Accept: application/json Content-Type: multipart/mixed; boundary=28ede98e-87b6-4701-b587-57bbcdccb802 Content-Length: 842 Host: API_CORRECT_IP Connection: 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
Postman 将内容发送为 multipart/form-data
,而 okhttp 设置为 multipart/mixed
。这个问题几乎可以肯定与 API.
意外的内容类型有关
尝试像这样设置生成器:
RequestBody requestBody = new MultipartBody.Builder()
.type(MultipartBody.FORM)
// ...
.build();
我需要向 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 returns 400,也就是说API没有识别到数据。 我在 Postiman 中尝试了相同的请求并且它工作正常,按预期返回 201。
我使用 wireshark 捕获了来自 postman 和来自模拟器的 http 请求。我在这些请求中找不到任何差异:
OkHttp 提出了这个请求:
8G^|E@@`}P~r#]'z@
m'<8POST /api/mobileinspector HTTP/1.1 Accept: application/json Content-Type: multipart/mixed; boundary=28ede98e-87b6-4701-b587-57bbcdccb802 Content-Length: 842 Host: API_CORRECT_IP Connection: Keep-Alive Accept-Encoding: gzip User-Agent: okhttp/3.9.1
--28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[fio]" Content-Length: 3
123 --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[email]" Content-Length: 11
123@eqw.sda --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[phone]" Content-Length: 7
7232321 --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[description]" Content-Length: 5
21321 --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[address]" Content-Length: 7
2332121 --28ede98e-87b6-4701-b587-57bbcdccb802 Content-Disposition: form-data; name="MobileInspector[category]" Content-Length: 1
2 --28ede98e-87b6-4701-b587-57bbcdccb802--
Postman 制作了这个:
8G^|E@@`}Pa8@
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
----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[fio]"
test ----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[email]"
test@example.com ----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[address]"
address ----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[phone]"
12312312 ----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[category]"
1 ----------------------------067684848634261464344219 Content-Disposition: form-data; name="MobileInspector[description]"
no description ----------------------------067684848634261464344219--
这两部分差异很大:
还行http:
m'<8POST /api/mobileinspector HTTP/1.1 Accept: application/json Content-Type: multipart/mixed; boundary=28ede98e-87b6-4701-b587-57bbcdccb802 Content-Length: 842 Host: API_CORRECT_IP Connection: 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
Postman 将内容发送为 multipart/form-data
,而 okhttp 设置为 multipart/mixed
。这个问题几乎可以肯定与 API.
尝试像这样设置生成器:
RequestBody requestBody = new MultipartBody.Builder()
.type(MultipartBody.FORM)
// ...
.build();