I am trying to read the body of a 400 response with the rest-client
gem. The problem is that rest-client
responds to 400 by throwing it as an error, so I can't figure out any way to get the body text.
Here's the motivating example. Consider this call to the facebook graph API:
JSON.parse(RestClient.get("https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token=#{access_token}"))
If the access_token
is expired or invalid, facebook does two things:
- Returns a 400 Bad Request HTTP response
- Returns JSON in the response body with more info, like this:
{
"error": {
"message": "The access token could not be decrypted",
"type": "OAuthException",
"code": 190
}
}
Because 400 response raises an Error, I can't figure out how to get the body of the response. That is, eg, if I run the GET request above in curl or in my browser, I can see the body, but I can't figure out how to access it in restclient. Here's an example:
begin
fb_response = JSON.parse(RestClient.get("https://graph.facebook.com/me?fields=id,email,first_name,last_name&access_token=#{access_token}"))
rescue => e
# 400 response puts me here
# How can I get the body of the above response now, so I can get details on the error?
# eg, was it an expired token? A malformed token? Something else?
end