Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace okhttp3.internal.http.HttpMethod.requiresRequestBody() #2687

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,36 @@ public Response send(Request request) throws Throwable {
}

byte[] entity = request.getEntity();
String requestMethod = request.getMethod().toString();
Method method = request.getMethod();
String methodValue = method.toString();
if (entity.length > 0) {
String contentType = request.getRequestHeaders().get("Content-Type");
MediaType mediaType = contentType != null
? MediaType.get(contentType + "; charset=utf-8")
: MEDIA_TYPE_APPLICATION_JSON;
RequestBody body = RequestBody.create(entity, mediaType);
requestBuilder.method(requestMethod, body);
requestBuilder.method(methodValue, body);
} else {
if (okhttp3.internal.http.HttpMethod.requiresRequestBody(requestMethod)) {
if (requiresRequestBody(method)) {
RequestBody body = RequestBody.create(entity, MEDIA_TYPE_TEXT_PLAIN);
requestBuilder.method(requestMethod, body);
requestBuilder.method(methodValue, body);
} else {
requestBuilder.method(requestMethod, null);
requestBuilder.method(methodValue, null);
}
}

okhttp3.Response response = client.newCall(requestBuilder.build()).execute();
return new Response(response.code(), response.body() == null ? null : response.body().string());
}

private static boolean requiresRequestBody(Method method) {
switch (method) {
case POST:
case PUT:
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved
return true;

default:
return false;
}
}
}