Class HTTPResponse

HTTPResponse

這個類別可讓使用者存取 HTTP 回應的特定資訊。

另請參閱

方法

方法傳回類型簡短說明
getAllHeaders()Object傳回 HTTP 回應標頭的屬性/值對應,並有標頭以陣列形式傳回多個值。
getAs(contentType)Blob傳回這個物件內的資料,做為轉換為指定內容類型的 blob。
getBlob()Blob以 blob 傳回這個物件內的資料。
getContent()Byte[]取得 HTTP 回應的原始二進位內容。
getContentText()String取得以字串編碼的 HTTP 回應內容。
getContentText(charset)String傳回編碼為指定字元集字串的 HTTP 回應內容。
getHeaders()Object傳回 HTTP 回應的標頭屬性/值對應。
getResponseCode()Integer取得 HTTP 回應的 HTTP 狀態碼 (200 代表 OK 等等)。

內容詳盡的說明文件

getAllHeaders()

傳回 HTTP 回應標頭的屬性/值對應,其中的標頭含有多個值做為陣列。

// The code below logs the HTTP headers from the response
// received when fetching the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getAllHeaders());

回攻員

Object:HTTP 標頭的 JavaScript 鍵/值對應


getAs(contentType)

傳回這個物件內的資料,做為轉換為指定內容類型的 blob。這個方法會在檔案名稱中加入適當的副檔名,例如「myfile.pdf」。但是,它假設檔案名稱後面緊接在最後句點的部分 (如有) 是應替換的現有副檔名。因此,「ShoppingList.12.25.2014」會變成「ShoppingList.12.25.pdf」。

如要查看轉換的每日配額,請參閱 Google 服務配額。新建的 Google Workspace 網域可能暫時適用更嚴格的配額。

參數

名稱類型說明
contentTypeString要轉換的 MIME 類型。對多數 blob 而言,'application/pdf' 是唯一的有效選項。如果是 BMP、GIF、JPEG 或 PNG 格式的圖片,'image/bmp''image/gif''image/jpeg''image/png' 中的任一個也是有效的格式。

回攻員

Blob:資料做為 blob。


getBlob()

以 blob 傳回這個物件內的資料。

回攻員

Blob:資料做為 blob。


getContent()

取得 HTTP 回應的原始二進位內容。

// The code below logs the value of the first byte of the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContent()[0]);

回攻員

Byte[]:以原始二進位陣列顯示的內容


getContentText()

取得以字串編碼的 HTTP 回應內容。

// The code below logs the HTML code of the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText());

回攻員

String:以字串表示的 HTTP 回應內容


getContentText(charset)

傳回編碼為指定字元集字串的 HTTP 回應內容。

// The code below logs the HTML code of the Google home page with the UTF-8 charset.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText("UTF-8"));

參數

名稱類型說明
charsetString字串,代表要用於編碼 HTTP 回應內容的字元集

回攻員

String:HTTP 回應的內容,使用指定的字元集進行編碼


getHeaders()

傳回 HTTP 回應的標頭屬性/值對應。

// The code below logs the HTTP headers from the response
// received when fetching the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getHeaders());

回攻員

Object:HTTP 標頭的 JavaScript 鍵/值對應


getResponseCode()

取得 HTTP 回應的 HTTP 狀態碼 (200 代表 OK 等等)。

// The code below logs the HTTP status code from the response received
// when fetching the Google home page.
// It should be 200 if the request succeeded.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getResponseCode());

回攻員

Integer — HTTP 回應代碼 (例如 200 代表 OK)