Class HTTPResponse

HTTPResponse

This class allows users to access specific information on HTTP responses.

See also

Methods

MethodReturn typeBrief description
getAllHeaders()ObjectReturns an attribute/value map of headers for the HTTP response, with headers that have multiple values returned as arrays.
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getBlob()BlobReturn the data inside this object as a blob.
getContent()Byte[]Gets the raw binary content of an HTTP response.
getContentText()StringGets the content of an HTTP response encoded as a string.
getContentText(charset)StringReturns the content of an HTTP response encoded as a string of the given charset.
getHeaders()ObjectReturns an attribute/value map of headers for the HTTP response.
getResponseCode()IntegerGet the HTTP status code (200 for OK, etc.) of an HTTP response.

Detailed documentation

getAllHeaders()

Returns an attribute/value map of headers for the HTTP response, with headers that have multiple values returned as arrays.


 // 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().toSource());
 

Return

Object — a JavaScript key/value map of HTTP headers


getAs(contentType)

Return the data inside this object as a blob converted to the specified content type. This method adds the appropriate extension to the filename — for example, "myfile.pdf". However, it assumes that the part of the filename that follows the last period (if any) is an existing extension that should be replaced. Consequently, "ChristmasList.12.25.2014" will become "ChristmasList.12.25.pdf".

Parameters

NameTypeDescription
contentTypeStringthe 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.

Return

Blob — the data as a blob


getBlob()

Return the data inside this object as a blob.

Return

Blob — the data as a blob


getContent()

Gets the raw binary content of an HTTP response.


 // 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]);
 

Return

Byte[] — the content as a raw binary array


getContentText()

Gets the content of an HTTP response encoded as a string.


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

Return

String — the content of the HTTP response, as a string


getContentText(charset)

Returns the content of an HTTP response encoded as a string of the given charset.


 // 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"));
 

Parameters

NameTypeDescription
charsetStringa string representing the charset to be used for encoding the HTTP response content

Return

String — the content of the HTTP response, encoded using the given charset


getHeaders()

Returns an attribute/value map of headers for the HTTP response.


 // 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().toSource());
 

Return

Object — a JavaScript key/value map of HTTP headers


getResponseCode()

Get the HTTP status code (200 for OK, etc.) of an HTTP response.


 // 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());
 

Return

Integer — HTTP response code (e.g. 200 for OK)

Send feedback about...

Apps Script
Apps Script