Class HTTPResponse

HTTPResponse

このクラスを使用すると、ユーザーは HTTP レスポンスに関する特定の情報にアクセスできます。

関連ドキュメント

Methods

メソッド戻り値の型概要
getAllHeaders()ObjectHTTP レスポンスのヘッダーの属性/値マップを返します。ヘッダーには複数の値が配列として返されます。
getAs(contentType)Blobこのオブジェクト内のデータを、指定されたコンテンツ タイプに変換された blob として返します。
getBlob()Blobこのオブジェクト内のデータを blob として返します。
getContent()Byte[]HTTP レスポンスの未加工のバイナリ コンテンツを取得します。
getContentText()String文字列にエンコードされた HTTP レスポンスの内容を取得します。
getContentText(charset)String指定された文字セットの文字列としてエンコードされた HTTP レスポンスの内容を返します。
getHeaders()ObjectHTTP レスポンスのヘッダーの属性/値マップを返します。
getResponseCode()IntegerHTTP レスポンスの HTTP ステータス コード(OK の場合は 200 など)を取得します。

詳細なドキュメント

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 Key-Value マップ


getAs(contentType)

このオブジェクト内のデータを、指定されたコンテンツ タイプに変換された blob として返します。この方法では、ファイル名に適切な拡張子(「myfile.pdf」など)が追加されます。ただし、ファイル名の最後のピリオド(存在する場合)に続く部分は、置き換えられる既存の拡張子であることを前提としています。この結果、「ShoppingList.12.25.2014」は「ShoppingList.12.25.pdf」になります。

コンバージョンの 1 日あたりの割り当てを確認するには、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"));

パラメータ

名前説明
charsetStringHTTP レスポンス コンテンツのエンコードに使用される文字セットを表す文字列

リターン

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 Key-Value マップ


getResponseCode()

HTTP レスポンスの HTTP ステータス コード(OK の場合は 200 など)を取得します。

// 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 レスポンス コード(例: OK の場合は 200)