Skip to content

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