class Patron::Response

Represents the response from the HTTP server.

def body_decodable?

Returns:
  • (Boolean) - true if the body is decodable, false if otherwise
def body_decodable?
  return true if @body.nil?
  return true if decoded_body
rescue HeaderCharsetInvalid, NonRepresentableBody
  false
end

def decoded_body

Returns:
  • (String, nil) -

Raises:
  • (Patron::HeaderCharsetInvalid) - when the server supplied a wrong or incorrect charset, {Patron::NonRepresentableBody}
def decoded_body
  return unless @body
  @decoded_body ||= decode_body(true)
end

def error?

Returns:
  • (Boolean) -
def error?
  status >= 400
end

def initialize(url, status, redirect_count, raw_header_data, body, default_charset = nil)

def initialize(url, status, redirect_count, raw_header_data, body, default_charset = nil)
  @url            = url.force_encoding(Encoding::ASCII) # the URL is always an ASCII subset, _always_.
  @status         = status
  @redirect_count = redirect_count
  @body           = body.force_encoding(Encoding::BINARY) if body
  header_data = decode_header_data(raw_header_data)
  parse_headers(header_data)
  @charset = charset_from_content_type
end

def inspect

Overridden so that the output is shorter and there is no response body printed
def inspect
  # Avoid spamming the console with the header and body data
  "#<Patron::Response @status_line='#{@status_line}'>"
end

def inspectable_body

Returns:
  • (String, nil) -

Other tags:
    See: Patron::Response#decoded_body -
def inspectable_body
  return unless @body
  @inspectable_body ||= decode_body(false)
end

def ok?

Returns:
  • (Boolean) -
def ok?
  !error?
end

def parse_headers(header_data_for_multiple_responses)

Called by the C code to parse and set the headers
def parse_headers(header_data_for_multiple_responses)
  @headers = {}
  responses = Patron::HeaderParser.parse(header_data_for_multiple_responses)
  last_response = responses[-1] # Only use the last response (for proxies and redirects)
  @status_line = last_response.status_line
  last_response.headers.each do |line|
    hdr, val = line.split(":", 2)
    val.strip! unless val.nil?
    if @headers.key?(hdr)
      @headers[hdr] = [@headers[hdr]] unless @headers[hdr].kind_of? Array
      @headers[hdr] << val
    else
      @headers[hdr] = val
    end
  end
end