OxL Справочник OxL
РусскийEnglish

data Client

The namespace of the client functions: Client.get(url). See the module documentation.

Example:

oxl✓ проверяется в CI
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 net
Sends a request and returns the response with its complete body.
openStream(method: str, url: str, headers: List<Header>, body: bytes, cfg: HttpConfig) -> ResponseStream | NetError net
Sends 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 net
GET url. A status of 4xx/5xx is a normal Response, not an error.
head(url: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
HEAD url: the status and headers without a body.
delete(url: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
DELETE url.
post(url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
POST url with body.
put(url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
PUT url with body.
patch(url: str, body: bytes, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
PATCH url with body.
postText(url: str, text: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
post with a UTF-8 text body.
putText(url: str, text: str, headers: List<Header>, cfg: HttpConfig) -> Response | NetError net
put with a UTF-8 text body.
connectionsOpened() -> i64
Connections opened so far by this process (all requests).
idleConnections() -> i64
Keep-alive connections waiting in the pool.
closeIdle()
Closes the idle pooled connections.

Определено в: std/http/http.oxl:429

См. также