spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.51k stars 3.31k forks source link

Gateway MVC: space character in POSTed JSON leads to IOException: insufficient data written #3401

Open mindhaq opened 4 months ago

mindhaq commented 4 months ago

Describe the bug

I'm using spring-boot 3.2.5 with spring cloud 2023.0.1 and JDK 21

I'm using Proxy Exchange Gateway with Spring MVC.

When I POST JSON data to a proxy controller that contains a space character, the proxy exchange fails with the exception in the title:

java.io.IOException: insufficient data written
    at java.base/sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3848) ~[na:na]
    at org.springframework.http.client.SimpleClientHttpRequest.executeInternal(SimpleClientHttpRequest.java:84) ~[spring-web-6.1.6.jar:6.1.6]
    at org.springframework.http.client.AbstractStreamingClientHttpRequest.executeInternal(AbstractStreamingClientHttpRequest.java:70) ~[spring-web-6.1.6.jar:6.1.6]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-6.1.6.jar:6.1.6]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:889) ~[spring-web-6.1.6.jar:6.1.6]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:740) ~[spring-web-6.1.6.jar:6.1.6]
    at org.springframework.cloud.gateway.mvc.ProxyExchange.exchange(ProxyExchange.java:347) ~[spring-cloud-gateway-mvc-4.1.2.jar:4.1.2]
    at org.springframework.cloud.gateway.mvc.ProxyExchange.post(ProxyExchange.java:308) ~[spring-cloud-gateway-mvc-4.1.2.jar:4.1.2]
    at com.mindhaq.demogatewayproxy.GatewayController.handlePost(GatewayController.kt:17) ~[main/:na]

       ....

With everything being the same, but not having any space characters in the JSON, there is no problem.

This was found while migrating a spring-boot monolith running on boot 2.2.x with netflix zuul which was updated to spring boot 3.2.x, cloud gateway, latest JDK etc. It took quite some time to reduce the error scenario to this.

Sample

I created a sample repository which proxies requests to httpbin.org

https://github.com/mindhaq/spring-demo-gateway-proxyexchange-mvc

This request fails with the exception above:

curl -X POST -H "Content-Type: application/json" -d '{"foo": "bar"}' http://localhost:8080/post 

This one works just fine:

curl -X POST -H "Content-Type: application/json" -d '{"foo":"bar"}' http://localhost:8080/post 

Going directly to httpbin.org works in both cases, so that is not the problem.

Do I miss any configuration or is this some deeper lying bug which got revealed by that space character?

mindhaq commented 4 months ago

It might be related to https://github.com/spring-cloud/spring-cloud-gateway/issues/3154

spencergibb commented 1 week ago

have you tried the fix for #3154?

spring-cloud-issues commented 1 day ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.