Skip to content

Commit c67bef8

Browse files
committed
Added support for spaces in path
1 parent 64918f3 commit c67bef8

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.http.client.utils.URIBuilder;
4747
import org.apache.http.client.utils.URLEncodedUtils;
4848
import org.apache.http.entity.ByteArrayEntity;
49+
import org.apache.http.entity.ContentType;
4950
import org.apache.http.entity.StringEntity;
5051
import org.apache.http.impl.client.HttpClientBuilder;
5152
import org.apache.http.util.EntityUtils;
@@ -99,8 +100,7 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options)
99100

100101
URI uri = new URIBuilder(request.url()).build();
101102

102-
// request url
103-
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getPath());
103+
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
104104

105105
// request query params
106106
List<NameValuePair> queryParams =
@@ -109,22 +109,14 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options)
109109
requestBuilder.addParameter(queryParam);
110110
}
111111

112-
// request body
113-
if (request.body() != null) {
114-
HttpEntity entity =
115-
request.charset() != null
116-
? new StringEntity(new String(request.body(), request.charset()))
117-
: new ByteArrayEntity(request.body());
118-
requestBuilder.setEntity(entity);
119-
}
120-
121112
// request headers
122113
boolean hasAcceptHeader = false;
123114
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
124115
String headerName = headerEntry.getKey();
125116
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
126117
hasAcceptHeader = true;
127118
}
119+
128120
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)
129121
&& requestBuilder.getHeaders(headerName) != null) {
130122
// if the 'Content-Length' header is already present, it's been set from HttpEntity, so we
@@ -141,9 +133,36 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options)
141133
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
142134
}
143135

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

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

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import feign.FeignException;
2727
import feign.Headers;
2828
import feign.Logger;
29+
import feign.Param;
2930
import feign.RequestLine;
3031
import feign.Response;
3132
import java.io.ByteArrayInputStream;
@@ -147,6 +148,23 @@ public void noResponseBody() {
147148
api.noPostBody();
148149
}
149150

151+
@Test
152+
public void postWithSpacesInPath() throws IOException, InterruptedException {
153+
server.enqueue(new MockResponse().setBody("foo"));
154+
155+
TestInterface api =
156+
Feign.builder()
157+
.client(new ApacheHttpClient())
158+
.target(TestInterface.class, "http://localhost:" + server.getPort());
159+
160+
Response response = api.post("current documents", "foo");
161+
162+
assertThat(server.takeRequest())
163+
.hasMethod("POST")
164+
.hasPath("/path/current%20documents/resource")
165+
.hasBody("foo");
166+
}
167+
150168
interface TestInterface {
151169

152170
@RequestLine("POST /?foo=bar&foo=baz&qux=")
@@ -163,5 +181,9 @@ interface TestInterface {
163181

164182
@RequestLine("POST")
165183
String noPostBody();
184+
185+
@RequestLine("POST /path/{to}/resource")
186+
@Headers("Accept: text/plain")
187+
Response post(@Param("to") String to, String body);
166188
}
167189
}

0 commit comments

Comments
 (0)