Skip to content

Commit 11bdf20

Browse files
committed
Merge pull request OpenFeign#274 from asukhyy/feign-httpclient-url-encoding-fix
Added support for spaces in path
2 parents 0904133 + 93cd0b7 commit 11bdf20

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

‎httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.http.client.utils.URIBuilder;
2828
import org.apache.http.client.utils.URLEncodedUtils;
2929
import org.apache.http.entity.ByteArrayEntity;
30+
import org.apache.http.entity.ContentType;
3031
import org.apache.http.entity.StringEntity;
3132
import org.apache.http.impl.client.HttpClientBuilder;
3233
import org.apache.http.util.EntityUtils;
@@ -101,30 +102,22 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
101102

102103
URI uri = new URIBuilder(request.url()).build();
103104

104-
//request url
105-
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getPath());
105+
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
106106

107107
//request query params
108108
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
109109
for (NameValuePair queryParam: queryParams) {
110110
requestBuilder.addParameter(queryParam);
111111
}
112112

113-
//request body
114-
if (request.body() != null) {
115-
HttpEntity entity = request.charset() != null ?
116-
new StringEntity(new String(request.body(), request.charset())) :
117-
new ByteArrayEntity(request.body());
118-
requestBuilder.setEntity(entity);
119-
}
120-
121113
//request headers
122114
boolean hasAcceptHeader = false;
123115
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
124116
String headerName = headerEntry.getKey();
125117
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
126118
hasAcceptHeader = true;
127119
}
120+
128121
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH) &&
129122
requestBuilder.getHeaders(headerName) != null) {
130123
//if the 'Content-Length' header is already present, it's been set from HttpEntity, so we
@@ -141,9 +134,36 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
141134
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
142135
}
143136

137+
//request body
138+
if (request.body() != null) {
139+
HttpEntity entity = null;
140+
if (request.charset() != null) {
141+
ContentType contentType = getContentType(request);
142+
String content = new String(request.body(), request.charset());
143+
entity = new StringEntity(content, contentType);
144+
} else {
145+
entity = new ByteArrayEntity(request.body());
146+
}
147+
148+
requestBuilder.setEntity(entity);
149+
}
150+
144151
return requestBuilder.build();
145152
}
146153

154+
private ContentType getContentType(Request request) {
155+
ContentType contentType = ContentType.DEFAULT_TEXT;
156+
for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
157+
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
158+
Collection values = entry.getValue();
159+
if (values != null && !values.isEmpty()) {
160+
contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
161+
break;
162+
}
163+
}
164+
return contentType;
165+
}
166+
147167
Response toFeignResponse(HttpResponse httpResponse) throws IOException {
148168
StatusLine statusLine = httpResponse.getStatusLine();
149169
int statusCode = statusLine.getStatusCode();

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717

1818
import com.squareup.okhttp.mockwebserver.MockResponse;
1919
import com.squareup.okhttp.mockwebserver.MockWebServer;
20-
import feign.Feign;
21-
import feign.FeignException;
22-
import feign.Headers;
23-
import feign.Logger;
24-
import feign.RequestLine;
25-
import feign.Response;
2620

2721
import org.junit.Rule;
2822
import org.junit.Test;
@@ -31,6 +25,14 @@
3125
import java.io.ByteArrayInputStream;
3226
import java.io.IOException;
3327

28+
import feign.Feign;
29+
import feign.FeignException;
30+
import feign.Headers;
31+
import feign.Logger;
32+
import feign.Param;
33+
import feign.RequestLine;
34+
import feign.Response;
35+
3436
import static feign.Util.UTF_8;
3537
import static feign.assertj.MockWebServerAssertions.assertThat;
3638
import static java.util.Arrays.asList;
@@ -143,6 +145,20 @@ public void noResponseBody() {
143145

144146
api.noPostBody();
145147
}
148+
@Test
149+
public void postWithSpacesInPath() throws IOException, InterruptedException {
150+
server.enqueue(new MockResponse().setBody("foo"));
151+
152+
TestInterface api = Feign.builder()
153+
.client(new ApacheHttpClient())
154+
.target(TestInterface.class, "http://localhost:" + server.getPort());
155+
156+
Response response = api.post("current documents", "foo");
157+
158+
assertThat(server.takeRequest()).hasMethod("POST")
159+
.hasPath("/path/current%20documents/resource")
160+
.hasBody("foo");
161+
}
146162

147163
interface TestInterface {
148164

@@ -160,5 +176,9 @@ interface TestInterface {
160176

161177
@RequestLine("POST")
162178
String noPostBody();
179+
180+
@RequestLine("POST /path/{to}/resource")
181+
@Headers("Accept: text/plain")
182+
Response post(@Param("to") String to, String body);
163183
}
164184
}

0 commit comments

Comments
 (0)