Class: HTTP::Message

Class | HTTP::Message
In: | lib/httpclient/http.rb
Parent: | Object

Represents a HTTP message. A message is for a request or a response.

Request message is generated from given parameters internally so users don‘t need to care about it. Response message is the instance that methods of HTTPClient returns so users need to know how to extract HTTP response data from Message.

Some attributes are only for a request or a response, not both.

## How to use HTTP response message

1. Gets response message body.

        res = clnt.get(url)
        p res.content #=> String

2. Gets response status code.

        res = clnt.get(url)
        p res.status #=> 200, 501, etc. (Integer)

3. Gets response header.

        res = clnt.get(url)
        res.header['set-cookie'].each do |value|
          p value
        end
        assert_equal(1, res.header['last-modified'].size)
        p res.header['last-modified'].first

### Methods

body= code content contenttype contenttype= dump file? internal_mime_type keep_alive_enabled? mime_type_handler mime_type_handler= multiparam_query? new_connect_request new_request new_response reason reason= status status= status_code version version=

### Classes and Modules

Class HTTP::Message::Body
Class HTTP::Message::Headers

### Constants

CRLF | = | "\r\n"

### External Aliases

mime_type_handler= | -> | set_mime_type_func
| For backward compatibility.
mime_type_handler | -> | get_mime_type_func

### Attributes

body | [R] |
HTTP::Message::Body: | message body.
header | [RW] |
HTTP::Message::Headers: | message header.
peer_cert | [RW] |
OpenSSL::X509::Certificate: | response only. server certificate which is used for retrieving the response.

### Public Class methods

file?(obj)

Returns true if the given object is a File. In HTTPClient, a file is;

- must respond to :read for retrieving String chunks.
- must respond to :path and returns a path for Content-Disposition.
- must respond to :pos and :pos= to rewind for reading. Rewinding is only needed for following HTTP redirect. Some IO impl defines :pos= but raises an Exception for pos= such as StringIO but there‘s no problem as far as using it for non-following methods (get/post/etc.)

internal_mime_type(path)

Default MIME type handler. See mime_type_handler=.

keep_alive_enabled?(version)

Returns true if the given HTTP version allows keep alive connection.

version: | Float

mime_type_handler()

Returns MIME type handler.

mime_type_handler=(handler)

Sets MIME type handler.

handler must respond to :call with a single argument :path and returns a MIME type String e.g. ‘text/html’. When the handler returns nil or an empty String, ‘application/octet-stream’ is used.

When you set nil to the handler, internal_mime_type is used instead. The handler is nil by default.

multiparam_query?(query)

Returns true if the given query (or body) has a multiple parameter.

new_connect_request(uri)

Creates a Message instance of ‘CONNECT’ request. ‘CONNECT’ request does not have Body.

uri: | an URI that need to connect. Only uri.host and uri.port are used.

new_request(method, uri, query = nil, body = nil, boundary = nil)

Creates a Message instance of general request.

method: | HTTP method String.
uri: | an URI object which represents an URL of web resource.
query: | a Hash or an Array of query part of URL. e.g. { "a" => "b" } => ‘host/part?a=b’ Give an array to pass multiple value like ["a", "b"], ["a", "c"] => ‘host/part?a=b&a=c‘
body: | a Hash or an Array of body part. e.g. { "a" => "b" } => ‘a=b’. Give an array to pass multiple value like ["a", "b"], ["a", "c"] => ‘a=b&a=c’.
boundary: | When the boundary given, it is sent as a multipart/form-data using this boundary String.

new_response(body)

Creates a Message instance of response.

body: | a String or an IO of response message body.

### Public Instance methods

body=(body)

Sets a new body. header.body_size is updated with new body.size.

code()

Alias for status

content()

Returns a content of message body. A String or an IO.

contenttype()

Sets ‘Content-Type’ header value. Overrides if already exists.

contenttype=(contenttype)

Returns ‘Content-Type’ header value.

dump(dev = '')

Dumps message (header and body) to given dev. dev needs to respond to <<.

reason()

Returns HTTP status reason phrase in response. String.

reason=(reason)

Sets HTTP status reason phrase of response. String.

status()

Returns HTTP status code in response. Integer.

status=(status)

Sets HTTP status code of response. Integer. Reason phrase is updated, too.

status_code()

Alias for status

version()

Returns HTTP version in a HTTP header. Float.

version=(version)

Sets HTTP version in a HTTP header. Float.

[Validate]