data BodyReader
A body read piece by piece: the body of a request (Request.stream(), works for buffered bodies too) or of a response
(ResponseStream.body()). Copyable; kind 0 = server request, 1 = HTTP/1.1 client response, 2 = HTTP/2 client response.
Example:
use std.http { Server, ServerConfig, ServerHandle, Router, Request, Reply, Client, Header, HttpConfig, Response, Middleware, Next, withMiddleware, requireHeader, logRequests, timing, WebSocket, WsConfig, SseWriter, BodyWriter, chunks }
fn body(req: Request) -> str {
match req.text() {
str t => t
DecodeError _ => ""
}
}
fn run() -> () | NetError | DecodeError | Panicked net, io, time {
let srv = Server.bind(ServerConfig{ port: 0, followNode: false })?
defer srv.close()
let stop = srv.handle()
let base = "http://127.0.0.1:{srv.port()}"
let router = Router.new().post("/up", (req: Request) => {
match req.stream().readAll() {
bytes all => Reply.text("{all.len()} bytes")
NetError _ => Reply.text("read failed", 400)
}
})
scope {
spawn stop.serve(router)
let r = Client.postText("{base}/up", "0123456789")?
print(r.text()?)
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Вывод
10 bytes
@derive(Copy)
pub(api) data BodyReaderПотокобезопасность. copyable; only one task should read a body at a time.
С версии. 1.0.0-beta
Методы
read | (max: i64, timeout: Duration?) -> bytes? | NetError netThe next piece of at most max bytes; none at the end of the body. |
readAll | (limit: i64, timeout: Duration?) -> bytes | NetError netAll the rest of the body; ProtocolError above limit bytes. |
Определено в: std/http/http.oxl:1236