Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9999

Spring RestTemplate does not capture response body when error code 40x is returned

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 3.1.2
    • Fix Version/s: None
    • Component/s: Web, Web:Client
    • Labels:
      None

      Description

      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.

        Attachments

          Activity

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              weiteh Wei Teh
              Last updater:
              Rossen Stoyanchev
            • Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 7 weeks ago