Skip to content

Commit

Permalink
Fixup: content-type of POST requests with files (#686)
Browse files Browse the repository at this point in the history
* Fixup: content-type of POST requests with files

* Using the opportunity to fix a typo
  • Loading branch information
wetneb authored May 15, 2022
1 parent ccc9a59 commit f2575db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public ApiConnection(String apiBaseUrl, Map<String, String> tokens) {
}

/**
* Subclasses can customized their own {@link OkHttpClient.Builder} instances.
* Subclasses can customize their own {@link OkHttpClient.Builder} instances.
*
* An example:
* <pre>
Expand Down Expand Up @@ -434,12 +434,14 @@ public InputStream sendRequest(String requestMethod,
} else if ("POST".equalsIgnoreCase(requestMethod)) {
RequestBody body;
if (files != null && !files.isEmpty()) {
MediaType formDataMediaType = MediaType.parse("multipart/form-data");
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(formDataMediaType);
parameters.entrySet().stream()
.forEach(entry -> builder.addFormDataPart(entry.getKey(), entry.getValue()));
files.entrySet().stream()
.forEach(entry -> builder.addFormDataPart(entry.getKey(), entry.getValue().getLeft(),
RequestBody.create(MediaType.parse("application/octet-stream"),entry.getValue().getRight())));
RequestBody.create(formDataMediaType,entry.getValue().getRight())));
body = builder.build();
} else {
body = RequestBody.create(queryString, URLENCODED_MEDIA_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
String requestBody = request.getBody().readUtf8();
// in the case of file uploads, the string representation of the request body is not stable
// so we only check that some file was uploaded (for testPostFile)
if (requestBody.contains("Content-Disposition: form-data; name=\"file\"; filename=\"hello.txt\"")) {
if (requestBody.contains("Content-Disposition: form-data; name=\"file\"; filename=\"hello.txt\"") &&
requestBody.contains("multipart/form-data")) {
return new MockResponse()
.setHeader("Content-Type", "application/json; charset=utf-8")
.setBody("{\"success\":\"true\"}");
Expand Down

0 comments on commit f2575db

Please sign in to comment.