Skip to content

Spring RestTemplate does not capture response body when error code 40x is returned [SPR-9999] #14633

@spring-projects-issues

Description

@spring-projects-issues

Wei Teh opened SPR-9999 and commented

When invoking rest endpoints with HTTP POST protocol via spring RestTemplate,
given error code 4xx is encountered, the response body is not captured and made available to the caller.

An example snippet code is as followed:

try {
  // invoking a rest endpoint that expects string object in returned,
  // even when the target rest endpoint returns a 4xx status code.
  String response = this.restTemplate.postForObject(url, form, String.class);
} catch (HtttpClientErrorException hcee) {
  // encountered 4xx problem from the server, but response body is not 
  // captured
  // the following assertion failed because the response body is expected to contain string values,   
  Assert.assertTrue(hcee.getResponseBodyAsByteArray().length > 0);
}

An example of response content (both header and body) is demonstrated with an invocation to the REST end point via curl: (response body contains exception name with name and message)

$ curl -id "username=blah&password=blah" http://localhost:8088/opensso/identity/authenticate
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   110  100    83  100    27   2677    870 --:--:-- --:--:-- --:--:--  5533HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: amlbcookie=01; Path=/
Content-Type: text/plain;charset=UTF-8
Content-Length: 83
Date: Fri, 16 Nov 2012 15:34:41 GMT

exception.name=com.sun.identity.idsvcs.InvalidCredentials Authentication Failed!!

Note the similar invocation using HTTP GET protocol works just fine (i.e. restTemplate.getForObject(..) ), as described in the forum thread attached in this jira ticket.


Affects: 3.1.2

Reference URL: http://forum.springsource.org/showthread.php?126907-Bug-in-Spring-3-1-1-RestTemplate-Class-in-retrieving-response-Buffer-on-401-error

2 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions