Reputation: 199
I need to send data to API and I use 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, that means API did not recognized data. I tried same request in Postiman and it worked fine, returned 201 as expected.
I captured http requests from postman and from simulator with wireshark. I can't find any differences in these requests:
OkHttp made this request:
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
[email protected] --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--
And Postman made this one:
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]"
[email protected] ----------------------------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--
Upvotes: 0
Views: 581
Reputation: 8467
Those two parts differ a lot:
okhttp:
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
Postman:
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 sends content as multipart/form-data
, while okhttp sets multipart/mixed
. This issue is almost certainly related to the content-type that is unexpected by API.
Try setting the builder like:
RequestBody requestBody = new MultipartBody.Builder()
.type(MultipartBody.FORM)
// ...
.build();
Upvotes: 1