method BodyReader.read
The next piece of at most max bytes; none at the end of the body. Waits at most timeout for data (default: no limit
beyond the connection's own deadlines, so pass one for slow clients).
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) => {
let r = req.stream()
var total = 0
var more = true
while more {
match r.read(4) {
bytes b => total += b.len()
NetError _ => more = false
none => more = false
}
}
Reply.text("{total}")
})
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")
_ => {}
}
}Output
10
pub(api) fn read(max: i64 = 65536, timeout: Duration? = none) -> bytes? | NetError netSince. 1.0.0-beta
Parameters
| Name | Type |
|---|---|
max | i64 = 65536 |
timeout | Duration? = none |
Returns
bytes?
Errors
NetError- `Timeout` when `timeout` passes, `Unavailable` (the peer went away), `ProtocolError`.
Effects
net
Defined in: std/http/http.oxl:1295