OxL Справочник OxL
РусскийEnglish

data Response

An HTTP response.

Example:

oxl✓ проверяется в CI
use std.http { Response, Header }

fn sample() -> Response {
  Response{
    status: 200, reason: "OK", version: "1.1",
    headers: [Header{ name: "Content-Type", value: "text/plain" }, Header{ name: "Set-Cookie", value: "a=1" }, Header{ name: "Set-Cookie", value: "b=2" }],
    body: Utf8.encode("hello"), url: "http://example.org/", reused: false, redirects: 0
  }
}

fn main() io {
  let r = sample()
  print("{r.status} {r.reason} {r.text()!}")
}
Вывод
200 OK hello
pub(api) data Response

Потокобезопасность. a plain value; safe to share once received.

С версии. 1.0.0-beta

Поля

pub(api) status: i64

Status code (200, 404, ...).

pub(api) reason: str

Reason phrase of the status line.

pub(api) version: str

1.0, 1.1 or 2.

pub(api) headers: List<Header>

Header fields in the order received.

pub(api) body: bytes

The complete body.

pub(api) url: str

The URL the response came from (after redirects).

pub(api) reused: bool

The request travelled on a pooled keep-alive connection.

pub(api) redirects: i64

Redirects followed.

Методы

ok() -> bool
Status 200..299.
header(name: str) -> str?
The first header with this name (case-insensitive).
headerAll(name: str) -> List<str>
All values of the header with this name, in order.
text() -> str | DecodeError
The body as UTF-8 text.
bodyLen() -> usize
The size of the body in bytes.

Определено в: std/http/http.oxl:177

См. также