Announcement Announcement Module
No announcement yet.
How to retrieve specific error message from REST request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to retrieve specific error message from REST request


    I am working on a StackOverflow integration with Spring Social - I have been able to complete the oauth dance and store the access token, however, when I then try to send a request to their API I get the error message logged:

    GET request for "" resulted in 400 (Bad Request); invoking error handler
    HttpClientErrorException while completing connection: 400 Bad Request
    Response body: ?

    I am trying to debug it, Can someone advise how to log the actual response message in the body? Im hoping i if i can find that it should be more straightforward, as I am expecting something like:

      error_message":"`key` is required when `access_token` is passed."
    Rather than just the high-level 400 error.


  • #2
    Ok, on further reading (here) it seems like the response is being GZipped - can someone advise how to use a MessageConverted to decompress the response?



    • #3
      I know of nothing of general use that handles GZipped responses. But Spring Android has stuff for that in its AbstractClientHttpResponse class. You might take a look at it ( to draw inspiration for how to handle GZipped responses.

      Regarding getting the actual response in the case of an error, the problem is that the default error handler treats any 4xx or 5xx responses as errors and usually eats them up and throws an exception...and you lose some context, such as the body of the response. I *strongly* recommend that anyone writing a Spring Social API binding create a custom error handler and inject it into the RestTemplate being used. For one thing, such an error handler can log error response bodies, helping in debugging problems such as yours. But also, it's a good thing to throw one of Spring Social's SocialExceptions instead of the more general exceptions thrown by RestTemplate. You can take a look at how Spring Social Twitter handles errors. Spring Social Facebook also has a custom error handler, but it may not be the best example...Facebook's errors are kind of a mess (but are getting better) so that error handler does some crazy stuff to deal with Facebook quirks.


      • #4
        FWIW, I've thought about this some more. It seems like it might be possible to handle GZipped content via an implementation of HttpMessageConverter. Maybe.

        What would need to happen, though, is for the GZip message converter to decompress the content and then, based on the content contained within the GZip file, choose another message converter to finish the conversion to whatever object is being asked for. This would make for a generally useful message converter...especially if it also worked in reverse for sending GZipped content.

        It's doubtful that I'll have a chance to explore this soon, but if you come up with something like this, it'd be great it you'd submit it as a pull request to the Spring Framework ( for possible inclusion in a future Spring release alongside the other message converters. At very least you should consider opening a JIRA issue against the Spring Framework asking for this capability.