Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Version 8.10
* OkHttpClient sends an empty POST body to conform with okhttp 2.4+

### Version 8.9
* Skips error handling when return type is `Response`

Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/feign/client/DefaultClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ protected void log(String configKey, String format, Object... args) {
api.post("foo");
}

@Test
public void noResponseBody() {
server.enqueue(new MockResponse());

TestInterface api = Feign.builder()
.target(TestInterface.class, "http://localhost:" + server.getPort());

api.noPostBody();
}

interface TestInterface {

@RequestLine("POST /?foo=bar&foo=baz&qux=")
Expand All @@ -196,5 +206,8 @@ interface TestInterface {
@RequestLine("PATCH /")
@Headers("Accept: text/plain")
String patch();

@RequestLine("POST")
String noPostBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import feign.Logger;
import feign.RequestLine;
import feign.Response;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -132,6 +133,17 @@ protected void log(String configKey, String format, Object... args) {
api.post("foo");
}

@Test
public void noResponseBody() {
server.enqueue(new MockResponse());

TestInterface api = Feign.builder()
.client(new ApacheHttpClient())
.target(TestInterface.class, "http://localhost:" + server.getPort());

api.noPostBody();
}

interface TestInterface {

@RequestLine("POST /?foo=bar&foo=baz&qux=")
Expand All @@ -145,5 +157,8 @@ interface TestInterface {
@RequestLine("PATCH /")
@Headers("Accept: text/plain")
String patch();

@RequestLine("POST")
String noPostBody();
}
}
10 changes: 9 additions & 1 deletion okhttp/src/main/java/feign/okhttp/OkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -77,7 +78,14 @@ static Request toOkHttpRequest(feign.Request input) {
requestBuilder.addHeader("Accept", "*/*");
}

RequestBody body = input.body() != null ? RequestBody.create(mediaType, input.body()) : null;
byte[] inputBody = input.body();
if ("POST".equals(input.method()) && inputBody == null) {
// write an empty BODY to conform with okhttp 2.4.0+
// http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/
inputBody = new byte[0];
}

RequestBody body = inputBody != null ? RequestBody.create(mediaType, inputBody) : null;
requestBuilder.method(input.method(), body);
return requestBuilder.build();
}
Expand Down
14 changes: 14 additions & 0 deletions okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ protected void log(String configKey, String format, Object... args) {
api.post("foo");
}

@Test
public void noResponseBody() {
server.enqueue(new MockResponse());

TestInterface api = Feign.builder()
.client(new OkHttpClient())
.target(TestInterface.class, "http://localhost:" + server.getPort());

api.noPostBody();
}

interface TestInterface {

@RequestLine("POST /?foo=bar&foo=baz&qux=")
Expand All @@ -147,5 +158,8 @@ interface TestInterface {
@RequestLine("PATCH /")
@Headers("Accept: text/plain")
String patch(String body);

@RequestLine("POST")
String noPostBody();
}
}