Skip to content

Commit 0904133

Browse files
pnepywodaAdrian Cole
authored andcommitted
Supports POST without a body parameter
```java @RequestLine("POST") String noPostBody(); ``` see http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/
1 parent 60c6ac3 commit 0904133

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Version 8.10
22
* Reads class-level @Produces/@Consumes JAX-RS annotations
3+
* Supports POST without a body parameter
34

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

‎core/src/test/java/feign/client/DefaultClientTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ protected void log(String configKey, String format, Object... args) {
183183
api.post("foo");
184184
}
185185

186+
@Test
187+
public void noResponseBody() {
188+
server.enqueue(new MockResponse());
189+
190+
TestInterface api = Feign.builder()
191+
.target(TestInterface.class, "http://localhost:" + server.getPort());
192+
193+
api.noPostBody();
194+
}
195+
186196
interface TestInterface {
187197

188198
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@@ -196,5 +206,8 @@ interface TestInterface {
196206
@RequestLine("PATCH /")
197207
@Headers("Accept: text/plain")
198208
String patch();
209+
210+
@RequestLine("POST")
211+
String noPostBody();
199212
}
200213
}

‎httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import feign.Logger;
2424
import feign.RequestLine;
2525
import feign.Response;
26+
2627
import org.junit.Rule;
2728
import org.junit.Test;
2829
import org.junit.rules.ExpectedException;
@@ -132,6 +133,17 @@ protected void log(String configKey, String format, Object... args) {
132133
api.post("foo");
133134
}
134135

136+
@Test
137+
public void noResponseBody() {
138+
server.enqueue(new MockResponse());
139+
140+
TestInterface api = Feign.builder()
141+
.client(new ApacheHttpClient())
142+
.target(TestInterface.class, "http://localhost:" + server.getPort());
143+
144+
api.noPostBody();
145+
}
146+
135147
interface TestInterface {
136148

137149
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@@ -145,5 +157,8 @@ interface TestInterface {
145157
@RequestLine("PATCH /")
146158
@Headers("Accept: text/plain")
147159
String patch();
160+
161+
@RequestLine("POST")
162+
String noPostBody();
148163
}
149164
}

‎okhttp/src/main/java/feign/okhttp/OkHttpClient.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.Reader;
28+
import java.util.Arrays;
2829
import java.util.Collection;
2930
import java.util.Map;
3031
import java.util.concurrent.TimeUnit;
@@ -77,7 +78,14 @@ static Request toOkHttpRequest(feign.Request input) {
7778
requestBuilder.addHeader("Accept", "*/*");
7879
}
7980

80-
RequestBody body = input.body() != null ? RequestBody.create(mediaType, input.body()) : null;
81+
byte[] inputBody = input.body();
82+
if ("POST".equals(input.method()) && inputBody == null) {
83+
// write an empty BODY to conform with okhttp 2.4.0+
84+
// http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/
85+
inputBody = new byte[0];
86+
}
87+
88+
RequestBody body = inputBody != null ? RequestBody.create(mediaType, inputBody) : null;
8189
requestBuilder.method(input.method(), body);
8290
return requestBuilder.build();
8391
}

‎okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ protected void log(String configKey, String format, Object... args) {
134134
api.post("foo");
135135
}
136136

137+
@Test
138+
public void noResponseBody() {
139+
server.enqueue(new MockResponse());
140+
141+
TestInterface api = Feign.builder()
142+
.client(new OkHttpClient())
143+
.target(TestInterface.class, "http://localhost:" + server.getPort());
144+
145+
api.noPostBody();
146+
}
147+
137148
interface TestInterface {
138149

139150
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@@ -147,5 +158,8 @@ interface TestInterface {
147158
@RequestLine("PATCH /")
148159
@Headers("Accept: text/plain")
149160
String patch(String body);
161+
162+
@RequestLine("POST")
163+
String noPostBody();
150164
}
151165
}

0 commit comments

Comments
 (0)