import {
"q2316367743/moonhttp/response",
}| 方法 | 说明 |
|---|---|
| bytes() | 原始响应字节,不解码(二进制内容、想自己按别的编码解时用) |
| text() | 按 config.response_encoding 解成文本;解码一律 lossy(坏字节 → U+FFFD),不抛错 |
| json() | 先按 text() 解码再 @json.parse;解析失败抛 @json.ParseError,不是 HttpError |
| content_length() | 响应体字节长度,等价 bytes().length() |
| is_success() | 状态码是否 2xx(给自定义了 validate_status 放行 3xx/4xx 的场景用) |
| 方法 | 说明 |
|---|---|
| with_status(status) | 换状态码(不重跑状态码校验) |
| with_headers(headers) | 整体替换响应头 |
| with_body(bytes) | 换响应体字节 |
| with_text(text) | 换响应体文本,按 response_encoding 编码——与 text() 读方向对称,with_text(response.text()) 恒等 |
| with_json(json) | 换 JSON 载荷(写回的是 JSON 文档:Json::String("x") 写成 "x" 带引号) |
let resp = api.get("/user/1")
println(resp.status) // 200
let user = resp.json()
println(resp.text())Install
Download zipaxios 风格的 MoonBit HTTP 客户端:实例与配置合并、四种请求体形态、流式与 SSE、自动重定向