data ResponseStream
A response whose head has arrived and whose body is read piece by piece (D-3970): Client.openStream (HTTP/1.1) and
Http2.openStream. close() it; closing before the end of the body abandons it (HTTP/1.1: the connection is closed, HTTP/2:
the stream is reset, other requests on the connection go on).
let rs = Http2.connect("https://example.org")?.openStream("GET", "/big")? // (keep the Http2 value to close it)
for await piece in chunks(rs.body()) { out.write(piece)? }Example:
use std.http { Server, ServerConfig, ServerHandle, Router, Request, Reply, Client, Header, HttpConfig, Response, WebSocket, WsConfig, WsKind, WsMessage, SseWriter, SseStream, SseConfig, SseEvent, BodyWriter, Http2, Http2Handle, ResponseStream, chunks }
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().get("/data", (req: Request) => Reply.text("0123456789"))
scope {
spawn stop.serve(router)
let rs = Client.openStream("GET", "{base}/data")?
defer rs.close()
print("{rs.status} {rs.version} {rs.ok()}")
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Output
200 1.1 true
@must_close
pub(api) data ResponseStreamThread safety. owned by the task that reads it.
Since. 1.0.0-beta
Fields
pub(api) status: i64 | The HTTP status of the response. |
pub(api) version: str |
|
pub(api) headers: List<Header> | Header fields of the response. |
Methods
body | () -> BodyReaderThe body reader (copyable) |
ok | () -> boolStatus 200..299. |
header | (name: str) -> str?The first header with this name (case-insensitive). |
close | ()Abandons or finishes the response |
Defined in: std/http/http.oxl:5159