method Client.openStream
Sends a request and returns once the response head has arrived; the body is read piece by piece from the ResponseStream
(D-3970). HTTP/1.1 on a connection of its own (Connection: close), no redirects, no pooling; timeout bounds connecting and
the head. For HTTP/2 use Http2.openStream.
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().get("/big", (req: Request) => Reply.text("0123456789"))
scope {
spawn stop.serve(router)
let rs = Client.openStream("GET", "{base}/big")?
defer rs.close()
let all = rs.body().readAll()?
print("{rs.status} {all.len()}")
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Output
200 10
pub(api) static fn openStream(method: str, url: str, headers: List<Header> = [], body: bytes = Utf8.encode(""), cfg: HttpConfig = HttpConfig{}) -> ResponseStream | NetError netSince. 1.0.0-beta
Parameters
| Name | Type |
|---|---|
method | str |
url | str |
headers | List<Header> = [] |
body | bytes = Utf8.encode("") |
cfg | HttpConfig = HttpConfig{} |
Returns
Errors
NetError- `NetError` as for `send`; the body errors surface while reading from the stream.
Effects
net
Defined in: std/http/http.oxl:546