Fix Zuul proxy POST on content-type with charset
Fixes gh-113
This commit is contained in:
@@ -8,6 +8,7 @@ import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.io.Charsets;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -45,10 +46,19 @@ public class FormBodyWrapperFilter extends ZuulFilter {
|
||||
public boolean shouldFilter() {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = ctx.getRequest();
|
||||
if (MediaType.APPLICATION_FORM_URLENCODED_VALUE.equals(request.getContentType())) {
|
||||
return true;
|
||||
String contentType = request.getContentType();
|
||||
|
||||
//Don't use this filter on GET method
|
||||
if(contentType == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Only use this filter for MediaType : application/x-www-form-urlencoded
|
||||
try {
|
||||
return MediaType.APPLICATION_FORM_URLENCODED.includes(MediaType.valueOf(contentType));
|
||||
} catch (InvalidMediaTypeException imte) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -68,6 +68,18 @@ public class FormZuulProxyApplicationTests {
|
||||
assertEquals("Posted! {foo=[bar]}", result.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithUTF8Form() {
|
||||
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
|
||||
form.set("foo", "bar");
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE+"; charset=UTF-8"));
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + port + "/simple", HttpMethod.POST,
|
||||
new HttpEntity<MultiValueMap<String,String>>(form, headers), String.class);
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
assertEquals("Posted! {foo=[bar]}", result.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
//Don't use @SpringBootApplication because we don't want to component scan
|
||||
|
||||
Reference in New Issue
Block a user