Response

Overview

The Response class provides utilities for managing HTTP responses in the Servoy environment, enabling efficient handling of HTTP interactions. It integrates with Servoy's scripting and JavaScript support, allowing for seamless debugging and interaction with HTTP data.

The class allows retrieval of HTTP status codes and their corresponding reason phrases, which is useful for identifying issues such as permission errors or malformed requests. For example, it can return status codes like 403 along with a reason phrase explaining the specific error. The response body can be accessed as a string or as binary data, with support for gzip-encoded content to handle a wide range of response formats.

Headers can be retrieved in a structured manner as key-value mappings. Additionally, developers can filter headers by specific names to extract targeted information. The getCharset method enables retrieval of the response body's character set, ensuring proper decoding for textual content. The class also provides error handling through the getException() method, which returns exception messages related to failed requests.

Methods Summarized

Type
Name
Summary

Should be called to delete temporary file that holds the response content.

Get the charset of the response body.

Getter for the exception message.

Gets a file upload object (that contains a temporary file), which can be transformed to a JSFile and then used by file plugin.

Get the content of response as binary data.

Get the content of the response as String.

Gets the headers of the response as name/value arrays.

Gets the headers of the response as name/value arrays.

Gets the status code of the response, the list of the possible values is in HTTP_STATUS constants.

Gets the status code's reason phrase.

Methods Detailed

close()

Should be called to delete temporary file that holds the response content. The temporary file is created only for bigger responses.

Returns: Boolean true if the temporary file with response data was deleted

getCharset()

Get the charset of the response body.

Returns: String

Sample

var charset = response.getCharset();

getException()

Getter for the exception message.

Returns: String the exception message

Sample

var exception = response.getException();

getFileUpload()

Gets a file upload object (that contains a temporary file), which can be transformed to a JSFile and then used by file plugin. If response body is too small, there is no file available (this should be used only for large files), otherwise use getResponseBody or getMediaData.

Returns: JSFileUpload fileupload object

Sample

plugins.file.onvertToJSFile(response.getFileUpload());

getMediaData()

Get the content of response as binary data. It also supports gzip-ed content. Note this loads all content in memory at once, for large files you should use getFileUpload which allows usage of a temporary file and streaming.

Returns: Array

Sample

var mediaData = response.getMediaData();

getResponseBody()

Get the content of the response as String.

Returns: String

Sample

var pageData = response.getResponseBody();

getResponseHeaders()

Gets the headers of the response as name/value arrays.

Returns: Object

Sample

var allHeaders = response.getResponseHeaders();
var header;

for (header in allHeaders) application.output(header + ': ' + allHeaders[header]);

getResponseHeaders(headerName)

Gets the headers of the response as name/value arrays.

Parameters

Returns: Object

Sample

var contentLength = response.getResponseHeaders("Content-Length");

getStatusCode()

Gets the status code of the response, the list of the possible values is in HTTP_STATUS constants.

In case there was an exception executing the request, please ignore/do not use this value (it will be 0). You can check that situation using response.getException().

Returns: Number

Sample

var status = response.getStatusCode();// compare with HTTP_STATUS constants

getStatusReasonPhrase()

Gets the status code's reason phrase. For example if a response contains status code 403 (Forbidden) it might be useful to know why.

For example a Jenkins API req. could answer with "403 No valid crumb was included in the request" which will let you know that you simply have to reques a crumb and then put that in the request headers as "Jenkins-Crumb". But you could not know that from 403 status alone...

Returns: String

Sample

var statusReasonPhrase = response.getStatusReasonPhrase();

Last updated