mkopylec / charon-spring-boot-starter

Reverse proxy implementation in form of a Spring Boot starter.
Apache License 2.0
240 stars 54 forks source link

Forwarding 'x-www-form-urlencoded' request removes form data #131

Closed jan-krakora closed 2 years ago

jan-krakora commented 2 years ago

Forwarding form data with POST request and Content-Type "application/x-www-form-urlencoded" does not work properly. Forwarded request does not contain the form data any more.

I have found that:

The org.springframework.http.RequestEntity is created within com.github.mkopylec.charon.forwarding.HttpRequestMapper and its body is always resolved like this

private byte[] extractBody(HttpServletRequest request) throws IOException {
    return toByteArray(request.getInputStream());
}

Problem with this approach is that "x-www-form-urlencoded" requests does not contain data within the InputStream. They are stored as params. See request.getParameterMap()

RequestEntity for "x-www-form-urlencoded" request can be constructed like this

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add...
map.add...

new RequestEntity<>(body, headers, method, uri);

Or is there any other way how to properly forward form requestes right now?

jan-krakora commented 2 years ago

I have found that in some filter before the charon's ReverseProxyFilter has been called request.getParameter() which in turn reads the request content. So the input stream for proxy is not available anymore.