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 (200 สำหรับ OK ฯลฯ) ของการตอบกลับ HTTP |
เอกสารโดยละเอียด
getAs(contentType)
แสดงผลข้อมูลภายในออบเจ็กต์นี้เป็น Blob ที่แปลงเป็นประเภทเนื้อหาที่ระบุ วิธีนี้จะเพิ่มนามสกุลที่เหมาะสมให้กับชื่อไฟล์ เช่น "myfile.pdf" อย่างไรก็ตาม ระบบจะ
ถือว่าส่วนของชื่อไฟล์ที่อยู่หลังจุดสุดท้าย (หากมี) เป็นนามสกุลที่มีอยู่
ซึ่งควรแทนที่ ดังนั้น "ShoppingList.12.25.2014" จะกลายเป็น "ShoppingList.12.25.pdf"
หากต้องการดูโควต้า Conversion รายวัน โปรดดูโควต้าสำหรับบริการของ 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 (200 สำหรับ OK ฯลฯ) ของการตอบกลับ 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());
รีเทิร์น
Integer
— รหัสการตอบกลับ HTTP (เช่น 200 สำหรับ OK)
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-08 UTC
[null,null,["อัปเดตล่าสุด 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)."]]