http 網路
畫面層、邏輯層都可用。使用方式與注意事項見網路與登入身分。
sdk.http.fetch()
ts
sdk.http.fetch(url: string, init?: HttpRequestInit): Promise<HttpResponse>
interface HttpRequestInit {
method?: string; // GET(預設)、POST、PUT、PATCH、DELETE、HEAD
headers?: Record<string, string>;
body?: string; // 文字;JSON 請自行 JSON.stringify
timeoutMs?: number; // 1000–60000,預設 15000
}
interface HttpResponse {
status: number;
headers: Record<string, string>; // 名稱一律小寫,不含 set-cookie
body: string; // UTF-8 文字
json<T = unknown>(): T; // JSON.parse(body);格式錯誤時拋 SyntaxError
}- 網址必須在
domains內(https、主機與埠完全相同),否則以DOMAIN_BLOCKED失敗。 - 非 2xx 不會 reject:自己判斷
status。 - 不帶 cookie、沒有 CORS 限制、不自動跟隨轉址(3xx 原樣回傳)。
參數規則
| 參數 | 規則 |
|---|---|
url | 最多 8192 字 |
method | 上表六種,大小寫不拘 |
headers | 最多 50 個;名稱為 token 字元,值為可見 ASCII 與 tab |
body | 最多 1 MB;GET、HEAD 不能帶 |
timeoutMs | 超出 1000–60000 會被夾到範圍內 |
被容器忽略的標頭:Host、Cookie、Cookie2、Connection、Content-Length、Transfer-Encoding、TE、Trailer、Upgrade、Keep-Alive、Expect、Origin、Referer、Via、Accept-Encoding、Proxy-*、Sec-*。
失敗
| code | 情況 |
|---|---|
DOMAIN_BLOCKED | 網址不在白名單(含相對網址、http://、IP) |
INVALID_PARAMS | 參數違反上表 |
TIMEOUT | 超過 timeoutMs |
NETWORK | 連線失敗、10 秒內建立不了連線、回應超過 5 MB |