Skip to content

RestTemplate headers not sent when bufferRequestBody is false [SPR-8809] #13451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Nov 1, 2011 · 8 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 1, 2011

Ryan Fong opened SPR-8809 and commented

I'm using Spring Core 3.1.0.RC1 on Sun Java 6 with Tomcat 7.0.14. I'm trying to send custom http headers when invoking RestTemplate methods using features in #10535 & #12152.

I configured an InterceptingClientHttpRequestFactory to use an interceptor that adds a header prior to execution. However, I've discovered that my header is not sent when the SimpleClientHttpRequestFactory bufferRequestBody property is false. I've also tried adding the header by setting an interceptor directly on the RestTemplate instead of via request factory but with the same result.

I've only been able to send the custom header by setting the SimpleClientHttpRequestFactory bufferRequestBody property back to its default of true. I need to avoid buffering due to bug #12564. Is this expected behavior? Are any workarounds available? Thank you!

references:
#10535 = RestTemplate - access to Request Headers
#12152 = Introduce interceptors for RestTemplate
#12564 = Sending large payloads with RestTemplate results in an OutOfMemoryError


Affects: 3.1 RC1

Reference URL: http://forum.springsource.org/showthread.php?115876-3.1-RestTemplate-headers-not-sent-when-bufferRequestBody-is-false

Referenced from: commits 53cb529, 91c14bd

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

This should be fixed now, can you please try a recent (as of tonight) snapshot and try again?

Thanks,

Arjen

@spring-projects-issues
Copy link
Collaborator Author

Ryan Fong commented

I downloaded org.springframework.web-3.1.0.CI-1162.jar and replaced only that file in my project but did not see any change in behavior. Do I need to change any other jars?

I'd also like to provide you with a test case of some sort but I'm not sure what the best structure is in this case. Please advise. Thanks.

@spring-projects-issues
Copy link
Collaborator Author

Ryan Fong commented

Apparently I did not grab the right file. The file I tested was dated 30 Jul 2011 (from http://static.springsource.org/downloads/nightly/snapshot-download.php?project=SPR) although this is the nightly link from the Spring Downloads webpage. Where is the correct non-Maven nightly snapshot download location?

@spring-projects-issues
Copy link
Collaborator Author

Ryan Fong commented

I located the correct build (1294) via the builds tab and tried it. I ran into the exception below. I reviewed the test you wrote and the interceptor has the same statement structure as my own. This problem occurs whether I use the InterceptingClientHttpRequestFactory or the RestTemplate.interceptors property.

Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error: cannot write to a URLConne
ction if doOutput=false - call setDoOutput(true); nested exception is java.net.ProtocolException: cannot write to a URLC
onnection if doOutput=false - call setDoOutput(true)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
at my.Main.main(Main.java:21)
Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:995)
at org.springframework.http.client.SimpleStreamingClientHttpRequest.getBodyInternal(SimpleStreamingClientHttpRequest.ja
va:83)
at org.springframework.http.client.AbstractClientHttpRequest.getBody(AbstractClientHttpRequest.java:44)
at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest
.java:88)
at my.spring.SharedSecretClientHttpRequestInterceptor.intercept(SharedSecretClientHttpRequestInterceptor.java:24)
at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest
.java:81)
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:67)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpReques
t.java:46)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:438)
... 3 more

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Ok, thanks for the feedback. I will investigate further.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Ok, I made some more changes, and I think it's fixed now. Can you try a recent snapshot (as of tomorrow) again to see if it works for you?

@spring-projects-issues
Copy link
Collaborator Author

Ryan Fong commented

G'day, Arjen. I confirmed that the header is now sent without buffering via an InterceptingClientHttpRequestFactory as of build CI-1303. Thank you!

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Great! Thanks for helping out.

@spring-projects-issues spring-projects-issues added type: bug A general bug in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.1 GA milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants