Class HTTPResponse
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
HTTPResponse
Lớp này cho phép người dùng truy cập vào thông tin cụ thể về các phản hồi HTTP.
Xem thêm
Phương thức
Phương thức | Kiểu dữ liệu trả về | Mô tả ngắn |
getAllHeaders() | Object | Trả về một bản đồ thuộc tính/giá trị của tiêu đề cho phản hồi HTTP, trong đó các tiêu đề có nhiều giá trị được trả về dưới dạng mảng. |
getAs(contentType) | Blob | Trả về dữ liệu bên trong đối tượng này dưới dạng một blob được chuyển đổi sang loại nội dung đã chỉ định. |
getBlob() | Blob | Trả về dữ liệu bên trong đối tượng này dưới dạng một blob. |
getContent() | Byte[] | Lấy nội dung nhị phân thô của một phản hồi HTTP. |
getContentText() | String | Lấy nội dung của một phản hồi HTTP được mã hoá dưới dạng một chuỗi. |
getContentText(charset) | String | Trả về nội dung của một phản hồi HTTP được mã hoá dưới dạng một chuỗi của bộ ký tự đã cho. |
getHeaders() | Object | Trả về một bản đồ thuộc tính/giá trị của các tiêu đề cho phản hồi HTTP. |
getResponseCode() | Integer | Nhận mã trạng thái HTTP (200 cho OK, v.v.) của một phản hồi HTTP. |
Tài liệu chi tiết
getAs(contentType)
Trả về dữ liệu bên trong đối tượng này dưới dạng một blob được chuyển đổi sang loại nội dung đã chỉ định. Phương thức này sẽ thêm đuôi thích hợp vào tên tệp, chẳng hạn như "myfile.pdf". Tuy nhiên, phương thức này giả định rằng phần tên tệp theo sau dấu chấm cuối cùng (nếu có) là một tiện ích hiện có cần được thay thế. Do đó, "ShoppingList.12.25.2014" sẽ trở thành "ShoppingList.12.25.pdf".
Để xem hạn mức hằng ngày cho lượt chuyển đổi, hãy xem Hạn mức cho các dịch vụ của Google. Các miền Google Workspace mới tạo có thể tạm thời phải tuân thủ hạn mức nghiêm ngặt hơn.
Thông số
Tên | Loại | Mô tả |
contentType | String | Loại MIME cần chuyển đổi. Đối với hầu hết các blob, 'application/pdf' là lựa chọn hợp lệ duy nhất. Đối với hình ảnh ở định dạng BMP, GIF, JPEG hoặc PNG, mọi định dạng 'image/bmp' , 'image/gif' , 'image/jpeg' hoặc 'image/png' cũng đều hợp lệ. Đối với tài liệu Google Tài liệu, 'text/markdown' cũng hợp lệ. |
Cầu thủ trả bóng
Blob
– Dữ liệu dưới dạng một blob.
getBlob()
Trả về dữ liệu bên trong đối tượng này dưới dạng một blob.
Cầu thủ trả bóng
Blob
– Dữ liệu dưới dạng một blob.
getContent()
Lấy nội dung nhị phân thô của một phản hồi 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]);
Cầu thủ trả bóng
Byte[]
– nội dung dưới dạng một mảng nhị phân thô
getContentText()
Lấy nội dung của một phản hồi HTTP được mã hoá dưới dạng một chuỗi.
// The code below logs the HTML code of the Google home page.
const response = UrlFetchApp.fetch('http://www.google.com/');
Logger.log(response.getContentText());
Cầu thủ trả bóng
String
– nội dung của phản hồi HTTP, dưới dạng một chuỗi
getContentText(charset)
Trả về nội dung của một phản hồi HTTP được mã hoá dưới dạng một chuỗi của bộ ký tự đã cho.
// 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'));
Thông số
Tên | Loại | Mô tả |
charset | String | một chuỗi biểu thị bộ ký tự sẽ được dùng để mã hoá nội dung phản hồi HTTP |
Cầu thủ trả bóng
String
– nội dung của phản hồi HTTP, được mã hoá bằng bộ ký tự đã cho
getResponseCode()
Nhận mã trạng thái HTTP (200 cho OK, v.v.) của một phản hồi HTTP.
// 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());
Cầu thủ trả bóng
Integer
– Mã phản hồi HTTP (ví dụ: 200 cho OK).
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-08 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-08 UTC."],[[["\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)."]]