spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

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

Open mindhaq opened this issue 1 year ago • 1 comments
trafficstars

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 avatar May 17 '24 22:05 mindhaq

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

mindhaq avatar May 18 '24 16:05 mindhaq

have you tried the fix for #3154?

spencergibb avatar Sep 27 '24 03:09 spencergibb

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.

spring-cloud-issues avatar Oct 04 '24 03:10 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Oct 11 '24 03:10 spring-cloud-issues