method Client.post
POST url with body.
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("/echo", (req: Request) => Reply.bytes(req.body, "application/octet-stream"))
scope {
spawn stop.serve(router)
let r = Client.post("{base}/echo", Utf8.encode("abc"))?
print("{r.status} {r.bodyLen()}")
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Вывод
200 3
pub(api) static fn post(url: str, body: bytes, headers: List<Header> = [], cfg: HttpConfig = HttpConfig{}) -> Response | NetError netС версии. 1.0.0-beta
Параметры
| Имя | Тип |
|---|---|
url | str |
body | bytes |
headers | List<Header> = [] |
cfg | HttpConfig = HttpConfig{} |
Возвращает
Ошибки
NetError- `NetError` as for `send`.
Эффекты
net
Определено в: std/http/http.oxl:734