net.fetch()
执行一次有界请求—响应。目的 host、方法和每次重定向都必须符合 manifest 与宿主策略。
Syntax
const response = await host.net.fetch(request, cancellation?);
Parameters
NetRequest {
url: bounded-https-url
method: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE"
headers?: HeaderMap
body?: ByteSource
timeoutMs?: u32
}
请求头是白名单;guest 不能自由设置 Cookie、Authorization 或连接控制头。
Return value
{ status: u16, headers: HeaderMap, body: Bytes, finalUrl?: bounded-https-url }
HTTP 4xx/5xx 正常返回,不抛 HostError。
Examples
const response = await host.net.fetch({
url: 'https://api.example.com/v1/items',
method: 'POST',
headers: { 'content-type': 'application/json' },
body: new TextEncoder().encode(JSON.stringify({ name: 'demo' })),
timeoutMs: 5_000,
});
if (response.status === 201) consume(response.body);
Errors
destination-denied、invalid-request、timeout、cancelled、response-too-large、redirect-denied、upstream-unavailable。
Requirements
- 能力:
host:net.fetch - manifest 中声明目的 host 和方法
- 信任档位:lime