You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This class provides methods that create a response of the scripted REST API. создают ответ для настроенного REST API.

Object instances of this type are available as a request variable in the API Action script body.

See the Scripted REST API article to learn more.

getBody()


Use the method to get the response body.

Return:

Type

Description

AnyThis method returns the response body.

Example:

getBody()
(function(request, response) {
  response.setBody({"key":"value"});
  const responseBody = response.getBody(); // {"key":"value"}
})(SimpleApiRequest, SimpleApiResponse)

getContentType()


Use the method to get the response body format.

Return:

Type

Description

StringThis method returns the response body format.

Example:

getContentType()
(function(request, response) {
  const contentType = response.getContentType(); // application/json
})(SimpleApiRequest, SimpleApiResponse)

getHeaders()


Use the method to get the value of all request headers.

Return:

TypeDescription
Object This method returns the object that contains the keys – the header names.

Example:

getHeaders()
(function(request, response) {
    // Отправляет запрос на
    // https://your-instance-url.simpleone.ru/v1/c_simple/api_module_path/api_action_path
 
    const allHeaders = response.getHeaders(); // {"accept-encoding":["gzip, deflate, br"],"postman-token":...}
})(SimpleApiRequest, SimpleApiResponse)

getStatus()


Use the method to get the response status.

Return:

TypeDescription
IntegerThis method returns the response status.

Example:

getStatus()
(function(request, response) {
  const status = response.getStatus(); // 200
})(SimpleApiRequest, SimpleApiResponse)

setBody(body)


Use the method to set the body content of the REST response.

Parameter():

НаименованиеТипОбязательноЗначение по умолчанию
bodyAnyДаНет

Возвращаемое значение:

ТипОписание
VoidМетод не возвращает значение.

Пример:

setBody()
(function(request, response) {
  response.setBody({"key":"value"});
  const responseBody = response.getBody(); // {"key":"value"}
})(SimpleApiRequest, SimpleApiResponse)

setContentType()


Используйте метод, чтобы задать формат тела ответа.

Параметры:

НаименованиеТипОбязательноЗначение по умолчанию
typeStringДаapplication/json

Возвращаемое значение:

ТипОписание
VoidМетод не возвращает значение.

Пример:

setContenType()
(function(request, response) {
  response.setContentType('text/plain');
})(SimpleApiRequest, SimpleApiResponse)

setHeader(header, value)


Используйте метод, чтобы задать значение заголовка REST ответа.

Параметры:

НаименованиеТипОбязательноЗначение по умолчанию
headerStringДаНет
valueAnyДаНет

Возвращаемое значение:

ТипОписание
VoidМетод не возвращает значение.

Пример:

setHeader()
(function (request, response) {
  response.setHeader('BACKEND-VERSION', ss.getProperty('simple.version.back'));
  response.setHeader('FRONTEND-VERSION', ss.getProperty('simple.version.front'));
})(SimpleApiRequest, SimpleApiResponse)

setStatus(status)


Используйте метод, чтобы задать кодовый номер HTTP статуса для ответа. Подробнее о возвращаемых статусах читайте в списке кодов.

Например, код статуса 200 говорит об успешном выполнении запроса, а 404 – запрошенный адрес URI не найден.

Параметры:

НаименованиеТипОбязательноЗначение по умолчанию
statusIntegerДа200

Возвращаемое значение:

ТипОписание
VoidМетод не возвращает значение.

Пример:

setStatus()
(function (request, response) {
  response.setBody({
    "support_phone": ss.getProperty('simple.auth_page.support_phone')
  });
  response.setStatus(200);
})(SimpleApiRequest, SimpleApiResponse)

  • No labels