Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Coerce the absence of body into the empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
WonderCsabo committed Nov 25, 2015
1 parent e90568d commit 0dc60e7
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class OkHttpClientHttpRequest extends AbstractBufferingClientHttpRequest

private static final String PROXY_AUTH_ERROR = "Received HTTP_PROXY_AUTH (407) code while not using proxy";

private static final byte[] NO_BODY = new byte[0];

private final OkHttpClient client;

private final URI uri;
Expand Down Expand Up @@ -77,8 +79,16 @@ public URI getURI() {
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] content) throws IOException {

MediaType contentType = getContentType(headers);
RequestBody body = (content.length > 0 ? RequestBody.create(contentType, content) : null);
boolean requiresBody = method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH;

RequestBody body;

if (requiresBody && content.length == 0) {
body = RequestBody.create(null, NO_BODY);
} else {
MediaType contentType = getContentType(headers);
body = (content.length > 0 ? RequestBody.create(contentType, content) : null);
}

URL url = this.uri.toURL();
String methodName = this.method.name();
Expand Down

0 comments on commit 0dc60e7

Please sign in to comment.