Skip to content
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

Avoid default Accept header from HttpUrlConnection in RestTemplate with put and delete #23740

Closed
nytro77 opened this issue Oct 1, 2019 · 5 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@nytro77
Copy link

nytro77 commented Oct 1, 2019

Affects: 5.1.10

It seems that the RestTemplate does not handle exceptions thrown by DELETE the same way as it does for GET and POST.
HttpStatusCodeException.getResponseBodyAsString() returns whitelabel error page as HTML instead of the expected JSON.

Example from exceptional DELETE:
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Tue Oct 01 15:38:04 CEST 2019</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>I am delete error</div></body></html>

Example from exceptional GET:
{"timestamp":"2019-10-01T13:38:04.682+0000","status":500,"error":"Internal Server Error","message":"I am get error","path":"/test"}

Reproduced in this simple Spring Boot project

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 1, 2019
@nytro77
Copy link
Author

nytro77 commented Oct 1, 2019

Hmmm... can this have something to do with it?
Found in SimpleBufferingClientHttpRequest.executeInternal method:

// JDK <1.8 doesn't support getOutputStream with HTTP DELETE
if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) {
this.connection.setDoOutput(false);
}

Spring 5.1 is Java 8 and later, so this code should be deleted anyways i guess?

@rstoyanchev
Copy link
Contributor

@nytro77 this sounds more like server-side behavior. getResponseBodyAsString() is a very simple convenience method to turn body from byte[] to String. It has no impact on what the format of the response is.

@rstoyanchev rstoyanchev added the status: waiting-for-feedback We need additional information before we can continue label Oct 16, 2019
@nytro77
Copy link
Author

nytro77 commented Oct 16, 2019

Ok, so i guess the error is in Spring Boot then? Or somewhere else in the Spring code?

I have configured nothing at all in the Spring Boot project that reproduces the problem. Just a simple @RestController like this:

@RestController
public class MyRestController {

	@DeleteMapping("/test")
	public void testDeleteError() {
		throw new RuntimeException("I am delete error");
	}

	@GetMapping("/test")
	public void testGetError() {
		throw new RuntimeException("I am get error");
	}
}

I tried with boot 2.2.0.RC1 and the result is the same.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 16, 2019
@nytro77
Copy link
Author

nytro77 commented Oct 16, 2019

I booted the application and called GET and DELETE for http://localhost:8080/test using curl.
They both return nice JSON.

curl -i -X GET http://localhost:8080/test
produces
{"timestamp":"2019-10-16T15:08:24.979+0000","status":500,"error":"Internal Server Error","message":"I am get error","path":"/test"}

curl -i -X DELETE http://localhost:8080/test
produces
{"timestamp":"2019-10-16T15:08:35.066+0000","status":500,"error":"Internal Server Error","message":"I am delete error","path":"/test"}

So i guess that there must be something strange with the RestTemplate after all?

@rstoyanchev
Copy link
Contributor

It looks like this is due to sun.net.www.protocol.http.HttpURLConnection setting the Accept header to "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" by default if not set.

For delete and put methods in the RestTemplate which return void we should probably set the Accept header to */* to allow for any error response.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 17, 2019
@rstoyanchev rstoyanchev added this to the 5.x Backlog milestone Oct 17, 2019
@rstoyanchev rstoyanchev added the status: ideal-for-contribution An issue that a contributor can help us with label Oct 17, 2019
@rstoyanchev rstoyanchev changed the title RestTemplate returns whitelabel error page HTML instead of JSON in error on DELETE Avoid default Accept header from HttpUrlConnection in RestTemplate with put and delete Oct 17, 2019
@rstoyanchev rstoyanchev added status: superseded An issue that has been superseded by another and removed status: ideal-for-contribution An issue that a contributor can help us with labels Nov 7, 2019
@rstoyanchev rstoyanchev removed this from the 5.x Backlog milestone Nov 7, 2019
rstoyanchev pushed a commit that referenced this issue Nov 8, 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) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants