Method: Patron::Response#decoded_body

Defined in:
lib/patron/response.rb

#decoded_bodyString?

Returns the response body converted into the Ruby process internal encoding (the one set as Encoding.default_internal). As the response gets returned, the response body is not assumed to be in any encoding whatsoever - it will be explicitly set to Encoding::BINARY (as if you were reading a file in binary mode).

When you call decoded_body, the method will look at the Content-Type response header, and check if that header specified a charset. If it did, the method will then check whether the specified charset is valid (whether it is possible to find a matching Encoding class in the VM). Once that succeeds, the method will check whether the response body is in the encoding that the server said it is.

This might not be the case - you can, for instance, easily serve an HTML document with a UTF-8 header (with the header being configured somewhere on the webserver level) and then have the actual HTML document override it with a meta element or charset containing an overriding charset. However, parsing the response body is outside of scope for Patron, so if this situation happens (the server sets a charset in the header but this header does not match what the server actually sends in the body) you will get an exception stating this is a problem.

The next step is actually converting the body to the internal Ruby encoding. That stage may raise an exception as well, if you are using an internal encoding which can't represent the response body faithfully. For example, if you run Ruby with a CJK internal encoding, and the response you are trying to decode uses Greek characters and is UTF-8, you are going to get an exception since it is impossible to coerce those characters to your internal encoding.

Returns:

  • (String, nil)

Raises:



95
96
97
98
# File 'lib/patron/response.rb', line 95

def decoded_body
  return unless @body
  @decoded_body ||= decode_body(true)
end