Skip to content

Commit e70bb4f

Browse files
DavidTanneradriancole
authored andcommitted
Add the Content-Type if specified, unless we are setting the body (OpenFeign#569)
* Add the Content-Type if specified, unless we are setting the body * Update changelog
1 parent c2bd91e commit e70bb4f

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Version 9.5.1
2+
* Update Okhttp client so that if specified, the content-type is included even without a body.
3+
14
### Version 9.5
25
* Introduces `feign-java8` with support for `java.util.Optional`
36
* Adds `Feign.Builder.mapAndDecode()` to allow response preprocessing before decoding it.

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ static Request toOkHttpRequest(feign.Request input) {
6262
}
6363

6464
for (String value : input.headers().get(field)) {
65+
requestBuilder.addHeader(field, value);
6566
if (field.equalsIgnoreCase("Content-Type")) {
6667
mediaType = MediaType.parse(value);
6768
if (input.charset() != null) {
6869
mediaType.charset(input.charset());
6970
}
70-
} else {
71-
requestBuilder.addHeader(field, value);
7271
}
7372
}
7473
}
@@ -79,10 +78,13 @@ static Request toOkHttpRequest(feign.Request input) {
7978

8079
byte[] inputBody = input.body();
8180
boolean isMethodWithBody = "POST".equals(input.method()) || "PUT".equals(input.method());
82-
if (isMethodWithBody && 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];
81+
if (isMethodWithBody) {
82+
requestBuilder.removeHeader("Content-Type");
83+
if (inputBody == null) {
84+
// write an empty BODY to conform with okhttp 2.4.0+
85+
// http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/
86+
inputBody = new byte[0];
87+
}
8688
}
8789

8890
RequestBody body = inputBody != null ? RequestBody.create(mediaType, inputBody) : null;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@
1616
package feign.okhttp;
1717

1818
import feign.Feign.Builder;
19+
import feign.Headers;
20+
import feign.Param;
21+
import feign.RequestLine;
22+
import feign.Response;
23+
import feign.Util;
24+
import feign.assertj.MockWebServerAssertions;
1925
import feign.client.AbstractClientTest;
2026

2127
import feign.Feign;
28+
import okhttp3.mockwebserver.MockResponse;
29+
import org.junit.Test;
30+
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
import static org.junit.Assert.assertEquals;
2235

2336
/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */
2437
public class OkHttpClientTest extends AbstractClientTest {
@@ -27,4 +40,29 @@ public class OkHttpClientTest extends AbstractClientTest {
2740
public Builder newBuilder() {
2841
return Feign.builder().client(new OkHttpClient());
2942
}
43+
44+
45+
@Test
46+
public void testContentTypeWithoutCharset() throws Exception {
47+
server.enqueue(new MockResponse()
48+
.setBody("AAAAAAAA"));
49+
OkHttpClientTestInterface api = newBuilder()
50+
.target(OkHttpClientTestInterface.class, "http://localhost:" + server.getPort());
51+
52+
Response response = api.getWithContentType();
53+
// Response length should not be null
54+
assertEquals("AAAAAAAA", Util.toString(response.body().asReader()));
55+
56+
MockWebServerAssertions.assertThat(server.takeRequest())
57+
.hasHeaders("Accept: text/plain", "Content-Type: text/plain") // Note: OkHttp adds content length.
58+
.hasMethod("GET");
59+
}
60+
61+
62+
public interface OkHttpClientTestInterface {
63+
64+
@RequestLine("GET /")
65+
@Headers({"Accept: text/plain", "Content-Type: text/plain"})
66+
Response getWithContentType();
67+
}
3068
}

0 commit comments

Comments
 (0)