HTTP 請求
Request 介面是 Fetch API 的一部分,代表 fetch() 的請求。
建構函式 跳到標題
Request() 建構函式會建立新的 Request 實例。
let request = new Request(resource, init);
參數 跳到標題
名稱 | 類型 | 選用 | 描述 |
---|---|---|---|
resource | Request 或 USVString |
否 |
資源可以是請求物件或 URL 字串。 |
init | RequestInit |
是 |
init 物件可讓您設定要套用至請求的選用參數。 |
傳回類型為 Request
實例。
RequestInit
跳到標題
名稱 | 類型 | 預設值 | 描述 |
---|---|---|---|
method |
string |
GET |
請求的方法。 |
headers |
Headers 或 { [key: string]: string } |
無 | 請求的標頭。 |
body |
Blob 、BufferSource 、FormData 、URLSearchParams 、USVString 或 ReadableStream |
無 | 請求的內容主體。 |
cache |
string |
無 | 請求的快取模式。 |
credentials |
string |
same-origin |
請求的憑證模式。 |
integrity |
string |
無 | 請求內容主體的加密雜湊值。 |
mode |
string |
cors |
您要使用的請求模式。 |
redirect |
string |
follow |
重新導向的處理模式。 |
referrer |
string |
about:client |
指定 no-referrer 、client 或 URL 的 USVString 。 |
屬性 跳到標題
名稱 | 類型 | 描述 |
---|---|---|
cache |
string |
快取模式指示瀏覽器應如何快取 (default 、no-cache 等) 請求。 |
credentials |
string |
憑證 (omit 、same-origin 等) 指示使用者代理程式是否應在請求的 CORS 情況下傳送 Cookie。 |
destination |
RequestDestination |
字串指示所請求內容的類型。 |
body |
ReadableStream |
Getter 會公開內容主體的 ReadableStream 。 |
bodyUsed |
boolean |
指示是否已讀取內容主體。 |
url |
USVString |
請求的 URL。 |
headers |
Headers |
與請求相關聯的標頭。 |
integrity |
string |
請求內容主體的加密雜湊值。 |
method |
string |
請求的方法 (POST 、GET 等)。 |
mode |
string |
指示請求的模式 (例如 cors )。 |
redirect |
string |
重新導向的處理模式。 |
referrer |
string |
請求的 referrer。 |
referrerPolicy |
string |
請求的 referrer 政策 |
以上所有屬性皆為唯讀。
方法 跳到標題
名稱 | 描述 |
---|---|
arrayBuffer() |
讀取內容主體串流至完成,並傳回 ArrayBuffer 物件。 |
blob() |
讀取內容主體串流至完成,並傳回 Blob 物件。 |
formData() |
讀取內容主體串流至完成,並傳回 FormData 物件。 |
json() |
讀取內容主體串流至完成,將其剖析為 JSON 並傳回 JavaScript 物件。 |
text() |
讀取內容主體串流至完成,並傳回 USVString 物件 (文字)。 |
clone() |
複製 Request 物件。 |
範例 跳到標題
function handler(_req) {
// Create a post request
const request = new Request("https://post.deno.dev", {
method: "POST",
body: JSON.stringify({
message: "Hello world!",
}),
headers: {
"content-type": "application/json",
},
});
console.log(request.method); // POST
console.log(request.headers.get("content-type")); // application/json
return fetch(request);
}
Deno.serve(handler);