class HTTP::Body

A request body, read from the connection as the handler asks for it.

<std:http>:298

Class methods

def self.new(reader: HTTP::Reader, length: Int?, chunked: Bool, limit: Int, on_first_read: () -> Void? = …) -> HTTP::Body

A body read from reader: length bytes, or chunked, accepting up to limit bytes. The server makes it; a test uses .of.

<std:http>:315

def self.of(text: String) -> HTTP::Body

A body with these contents (for requests built in tests).

<std:http>:322

def self.of(bytes: Bytes) -> HTTP::Body

A body with these contents (for requests built in tests).

<std:http>:325

def self.empty -> HTTP::Body

An empty body.

<std:http>:330

Methods

def size -> Int?

The length of the body in bytes as its Content-Length gives it, or nil when the request has none (a chunked body, or a parsed request with no body). A body made by .of has its bytes' size.

<std:http>:335

def consumed? -> Bool

Whether it has been read to the end.

<std:http>:338

def started? -> Bool

Whether reading has begun (a 100 Continue has been sent, if asked).

<std:http>:341

def failed? -> Bool

Whether a read failed (bad framing, the client stopped sending).

<std:http>:344

def remaining -> Int?

How much of a Content-Length body is left (nil when chunked).

<std:http>:347

def read_chunk(max: Int = …) -> Bytes

The next piece of the body, up to max bytes; empty at its end.

<std:http>:350

def each_chunk(&: (Bytes) -> Void) -> Void

Yields the body's pieces as they arrive, until its end. Unlike #read, it is not bounded by max_body_bytes.

<std:http>:385

def read_bytes -> Bytes

The whole body; PayloadTooLarge past the server's max_body_bytes.

<std:http>:394

def read -> String

The whole body as a string. Raises PayloadTooLarge past the server's max_body_bytes and BadRequest when it is not UTF-8. What is read is gone: a second read gives "".

body = HTTP::Body.of("name=quarz")
body.size    # => 10
body.read    # => "name=quarz"
body.read    # => ""

<std:http>:415

def drain(max: Int) -> Bool

Read what is left, up to max bytes; whether the body ended.

<std:http>:423