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

Comments

@nytro77
Copy link

@nytro77 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

@nytro77

This comment has been minimized.

Copy link
Author

@nytro77 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

This comment has been minimized.

Copy link
Contributor

@rstoyanchev rstoyanchev commented Oct 16, 2019

@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.

@nytro77

This comment has been minimized.

Copy link
Author

@nytro77 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.

@nytro77

This comment has been minimized.

Copy link
Author

@nytro77 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

This comment has been minimized.

Copy link
Contributor

@rstoyanchev rstoyanchev commented Oct 17, 2019

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 this to the 5.x Backlog milestone 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 removed this from the 5.x Backlog milestone Nov 7, 2019
@rstoyanchev rstoyanchev closed this Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.