Class HTTPResponse
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
HTTPResponse
此类允许用户访问 HTTP 响应中的特定信息。
另请参阅
详细文档
getAs(contentType)
以转换为指定内容类型的 blob 形式返回此对象内的数据。此方法会向文件名添加适当的扩展名,例如“myfile.pdf”。不过,它会假设文件名中最后一个句点(如果有)后面的部分是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”会变成“ShoppingList.12.25.pdf”。
如需查看转化方面的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能暂时受到更严格的配额限制。
参数
名称 | 类型 | 说明 |
contentType | String | 要转换成的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于采用 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp' 、'image/gif' 、'image/jpeg' 或 'image/png' 中的任何一个也有效。对于 Google 文档,'text/markdown' 也有效。 |
返回
Blob
- 以 blob 形式表示的数据。
getBlob()
以 blob 形式返回此对象内的数据。
返回
Blob
- 以 blob 形式表示的数据。
getContent()
获取 HTTP 响应的原始二进制内容。
// The code below logs the value of the first byte of the Google home page.
const 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.
const 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.
const response = UrlFetchApp.fetch('http://www.google.com/');
Logger.log(response.getContentText('UTF-8'));
参数
名称 | 类型 | 说明 |
charset | String | 一个字符串,表示用于对 HTTP 响应内容进行编码的字符集 |
返回
String
- HTTP 响应的内容,使用给定的字符集进行编码
getResponseCode()
获取 HTTP 响应的 HTTP 状态代码(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.
const response = UrlFetchApp.fetch('http://www.google.com/');
Logger.log(response.getResponseCode());
返回
Integer
- HTTP 响应代码(例如,200 表示“正常”)。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-08。
[null,null,["最后更新时间 (UTC):2025-08-08。"],[[["\u003cp\u003eThe \u003ccode\u003eHTTPResponse\u003c/code\u003e class provides access to data and metadata returned from an HTTP request made by \u003ccode\u003eUrlFetchApp\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt offers methods to retrieve content as raw bytes, string, or blob, and to get headers or the response code.\u003c/p\u003e\n"],["\u003cp\u003eContent can be retrieved in various formats, including blob and string, with options for content type conversion.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can access HTTP headers to get detailed response information for debugging or further processing.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eHTTPResponse\u003c/code\u003e helps developers work with HTTP responses received by \u003ccode\u003eUrlFetchApp\u003c/code\u003e within Apps Script.\u003c/p\u003e\n"]]],[],null,["# Class HTTPResponse\n\nHTTPResponse\n\nThis class allows users to access specific information on HTTP responses.\n\n#### See also\n\n- [UrlFetchApp](/apps-script/reference/url-fetch/url-fetch-app) \n\n### Methods\n\n| Method | Return type | Brief description |\n|----------------------------------------------------|---------------------------|-----------------------------------------------------------------------------------------------------------------------------|\n| [getAllHeaders()](#getAllHeaders()) | `Object` | Returns an attribute/value map of headers for the HTTP response, with headers that have multiple values returned as arrays. |\n| [getAs(contentType)](#getAs(String)) | [Blob](../base/blob.html) | Return the data inside this object as a blob converted to the specified content type. |\n| [getBlob()](#getBlob()) | [Blob](../base/blob.html) | Return the data inside this object as a blob. |\n| [getContent()](#getContent()) | `Byte[]` | Gets the raw binary content of an HTTP response. |\n| [getContentText()](#getContentText()) | `String` | Gets the content of an HTTP response encoded as a string. |\n| [getContentText(charset)](#getContentText(String)) | `String` | Returns the content of an HTTP response encoded as a string of the given charset. |\n| [getHeaders()](#getHeaders()) | `Object` | Returns an attribute/value map of headers for the HTTP response. |\n| [getResponseCode()](#getResponseCode()) | `Integer` | Get the HTTP status code (200 for OK, etc.) of an HTTP response. |\n\nDetailed documentation\n----------------------\n\n### `get``All``Headers()`\n\nReturns an attribute/value map of headers for the HTTP response, with headers that have\nmultiple values returned as arrays.\n\n```javascript\n// The code below logs the HTTP headers from the response\n// received when fetching the Google home page.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getAllHeaders());\n```\n\n#### Return\n\n\n`Object` --- a JavaScript key/value map of HTTP headers\n\n*** ** * ** ***\n\n### `get``As(contentType)`\n\nReturn the data inside this object as a blob converted to the specified content type. This\nmethod adds the appropriate extension to the filename---for example, \"myfile.pdf\". However, it\nassumes that the part of the filename that follows the last period (if any) is an existing\nextension that should be replaced. Consequently, \"ShoppingList.12.25.2014\" becomes\n\"ShoppingList.12.25.pdf\".\n\nTo view the daily quotas for conversions, see [Quotas for Google\nServices](https://developers.google.com/apps-script/guides/services/quotas). Newly created Google Workspace domains might be temporarily subject to stricter\nquotas.\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `content``Type` | `String` | The MIME type to convert to. For most blobs, `'application/pdf'` is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of `'image/bmp'`, `'image/gif'`, `'image/jpeg'`, or `'image/png'` are also valid. For a Google Docs document, `'text/markdown'` is also valid. |\n\n#### Return\n\n\n[Blob](../base/blob.html) --- The data as a blob.\n\n*** ** * ** ***\n\n### `get``Blob()`\n\nReturn the data inside this object as a blob.\n\n#### Return\n\n\n[Blob](../base/blob.html) --- The data as a blob.\n\n*** ** * ** ***\n\n### `get``Content()`\n\nGets the raw binary content of an HTTP response.\n\n```javascript\n// The code below logs the value of the first byte of the Google home page.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getContent()[0]);\n```\n\n#### Return\n\n\n`Byte[]` --- the content as a raw binary array\n\n*** ** * ** ***\n\n### `get``Content``Text()`\n\nGets the content of an HTTP response encoded as a string.\n\n```javascript\n// The code below logs the HTML code of the Google home page.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getContentText());\n```\n\n#### Return\n\n\n`String` --- the content of the HTTP response, as a string\n\n*** ** * ** ***\n\n### `get``Content``Text(charset)`\n\nReturns the content of an HTTP response encoded as a string of the given charset.\n\n```javascript\n// The code below logs the HTML code of the Google home page with the UTF-8\n// charset.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getContentText('UTF-8'));\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------|----------|-------------------------------------------------------------------------------------|\n| `charset` | `String` | a string representing the charset to be used for encoding the HTTP response content |\n\n#### Return\n\n\n`String` --- the content of the HTTP response, encoded using the given charset\n\n*** ** * ** ***\n\n### `get``Headers()`\n\nReturns an attribute/value map of headers for the HTTP response.\n\n```javascript\n// The code below logs the HTTP headers from the response\n// received when fetching the Google home page.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getHeaders());\n```\n\n#### Return\n\n\n`Object` --- a JavaScript key/value map of HTTP headers\n\n*** ** * ** ***\n\n### `get``Response``Code()`\n\nGet the HTTP status code (200 for OK, etc.) of an HTTP response.\n\n```javascript\n// The code below logs the HTTP status code from the response received\n// when fetching the Google home page.\n// It should be 200 if the request succeeded.\nconst response = UrlFetchApp.fetch('http://www.google.com/');\nLogger.log(response.getResponseCode());\n```\n\n#### Return\n\n\n`Integer` --- The HTTP response code (for example, 200 for OK)."]]