data Client
The namespace of the client functions: Client.get(url). See the module documentation.
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("/ping", (req: Request) => Reply.text("pong"))
scope {
spawn stop.serve(router)
let r = Client.get("{base}/ping")?
print("{r.status} {r.text()?}")
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Вывод
200 pong
pub(api) data ClientПотокобезопасность. safe from any task; the connection pool is shared by the process.
С версии. 1.0.0-beta
Методы
send | (method: str, url: str, headers: List<Header>, body: bytes, cfg: HttpConfig) -> Response | NetError netSends a request and returns the response with its complete body. |
openStream | (method: str, url: str, headers: List<Header>, body: bytes, cfg: HttpConfig) -> ResponseStream | NetError netSends a request and returns once the response head has arrived; the body is read piece by piece from the ResponseStream (D-3970). |
get | (url: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netGET url. A status of 4xx/5xx is a normal Response, not an error. |
head | (url: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netHEAD url: the status and headers without a body. |
delete | (url: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netDELETE url. |
post | (url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netPOST url with body. |
put | (url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netPUT url with body. |
patch | (url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netPATCH url with body. |
postText | (url: str, text: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netpost with a UTF-8 text body. |
putText | (url: str, text: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError netput with a UTF-8 text body. |
connectionsOpened | () -> i64Connections opened so far by this process (all requests). |
idleConnections | () -> i64Keep-alive connections waiting in the pool. |
closeIdle | ()Closes the idle pooled connections. |
Определено в: std/http/http.oxl:429