Cloud Software Group, Inc. EBX®
Documentation > Developer Guide > EBX® Script > API
Navigation modeDocumentation > Developer Guide > EBX® Script > API

Unit core.http

Script unit that provides methods to call an external REST service.

Methods

procedure addParameter(request: http_request, parameter: string, value: string)

Adds a query parameter to the request.

procedure addPath(request: http_request, path: string)

Adds a path to the url of the request.

procedure addSegment(request: http_request, segment: string)

Adds a segment to the url of the request.

function createRequest(id: string): http_request

Creates a request specified by the configuration id.

function delete(request: http_request): http_response

Sends a DELETE request and handle the response.

function get(request: http_request): http_response

Sends a GET request and handle the response.

function getConfigNames(request: http_request): list<string>

Gets the key names specified in the rest configuration.

function getConfigValue(request: http_request, name: string): string

Gets the value of a given key name specified in the rest configuration.

function getHeaderFirstValue(response: http_response, headerName: string): string

Returns the first response header value of the given named header.

function getHeaderNames(response: http_response): list<string>

Returns the response header names.

function getHeaderValues(response: http_response, headerName: string): list<string>

Returns the response header values of the given named header.

function getJsonBody(response: http_response): json_value

Returns a JSON value of the body response.

function getRequestUrl(request: http_request): string

Gets the url of the request.

function getResponseUrl(response: http_response): string

Gets the url of the response.

function getStatusCode(response: http_response): int

Returns the status code of the response.

function getStringBody(response: http_response): string

Returns a string body of the response.

function hasBody(response: http_response): boolean

Returns true if the response has a body.

function post(request: http_request, body: string): http_response

Sends a POST request by setting the given body and handle the response.

function put(request: http_request, body: string): http_response

Sends a PUT request by setting the given body and handle the response.

procedure setHeader(request: http_request, headerField: string, value: string)

Sets a value to the header field of the request.

procedure setTimeout(request: http_request,timeout: int)

Sets a timeout to the request in milliseconds.

addParameter

procedure addParameter(request: http_request, parameter: string, value: string)

Adds a query parameter to the request. The parameter value is percentage encoded.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book');
  client.addParameter(request,'status','sold');
  ...
end

Parameters :

request: the request.

parameter: the query parameter.

value: the parameter value.

Can be used in: 

addPath

procedure addPath(request: http_request, path: string)

Adds a path to the url of the request. Note that this method does not encode the given path.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book/findByStatus');
  ...
end

Parameters :

request: the request.

path: the path to be added.

Can be used in: 

addSegment

procedure addSegment(request: http_request, segment: string)

Adds a segment to the url of the request. Note that the given segment is percentage encoded.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addSegment(request,'search by term');
  ...
end

Parameters :

request: the request.

segment: the segment to be added.

Can be used in: 

createRequest

function createRequest(id: string): http_request

Creates a request specified by the configuration id. Raises an error if the configuration id is not found. The id is specified in the configuration table of the EBX administration.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  ...
end

Parameters :

id: the id of the configuration.

Return :

the request.

Can be used in: 

delete

function delete(request: http_request): http_response

Sends a DELETE request and handle the response. Raises an error if an I/O error occurs or the request times out. The returned response has status, headers and maybe a body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book');
  client.addPath(request,'B5253');
  var response := client.delete(request);
  ...
end

Parameters :

request: the request.

Return :

the received response.

Can be used in: 

get

function get(request: http_request): http_response

Sends a GET request and handle the response. Raises an error if an I/O error occurs or the request times out. The returned response has status, headers and a body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.setHeader(request,'Content-Type','application/json');
  var response := client.get(request);
  ...
end

Parameters :

request: the request.

Return :

the received response.

Can be used in: 

getConfigNames

function getConfigNames(request: http_request): list<string>

Gets the key names specified in the rest configuration. Returns an empty list if no key name is found.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var keyNames := client.getConfigNames(request);
  var keyValue := client.getConfigValue(request,keyNames[0]);
  ...
end

Parameters :

request: the request.

Return :

the string value.

Can be used in: 

getConfigValue

function getConfigValue(request: http_request, name: string): string

Gets the value of a given key name specified in the rest configuration. The value of a secret key is always decrypted. Returns null if the key name is not found. Raises an error if the value of the given key can not be decrypted.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var accessKey := client.getConfigValue(request,'EBX');
  if not isNull(accessKey) then
  begin
    client.setHeader(request,'Authorization','EBX '| accessKey);
    ...
  end
end

Parameters :

request: the request.

Return :

the string value.

Can be used in: 

getHeaderFirstValue

function getHeaderFirstValue(response: http_response, headerName: string): string

Returns the first response header value of the given named header. Returns null if the header name is not found.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  var encoding := client.getHeaderFirstValue(response,'content-encoding');
  ...
end

Parameters :

response: the response.

headerName: the header name.

Return :

the header values.

Can be used in: 

getHeaderNames

function getHeaderNames(response: http_response): list<string>

Returns the response header names. Returns an empty list if no named header is found.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  var headerNames := client.getHeaderNames(response);
  ...
end

Parameters :

response: the response.

Return :

a list of the header names.

Can be used in: 

getHeaderValues

function getHeaderValues(response: http_response, headerName: string): list<string>

Returns the response header values of the given named header. Returns an empty list if the name is not found.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  var allowedMethod := client.getHeaderValues(response,'access-control-allow-methods');
  ...
end

Parameters :

response: the response.

headerName: the header name.

Return :

a list of the header values.

Can be used in: 

getJsonBody

function getJsonBody(response: http_response): json_value

Returns a JSON value of the body response. Returns null if the response has no body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  if (client.hasBody(response)) then
  begin
    var body:=client.getJsonBody(response).asObject;
    var id := json.getString(body, 'id');
    ...
  end
end

Parameters :

response: the response.

Return :

the JSON value of the body response. If the body of the response is not valid JSON or is null, returns null.

Can be used in: 

getRequestUrl

function getRequestUrl(request: http_request): string

Gets the url of the request.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'author');
  var url:=client.getRequestUrl(response);
end

Parameters :

request: the request.

Return :

the url.

Can be used in: 

getResponseUrl

function getResponseUrl(response: http_response): string

Gets the url of the response.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'publisher');
  var response := client.get(request);
  var url:=client.getResponseUrl(response);
end

Parameters :

response: the response.

Return :

the url.

Can be used in: 

getStatusCode

function getStatusCode(response: http_response): int

Returns the status code of the response.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book');
  var response := client.get(request);
  var status:=client.getStatusCode(response);
  if (status = 200) then
  begin
    ...
  end
end

Parameters :

response: the response.

Return :

the status code.

Can be used in: 

getStringBody

function getStringBody(response: http_response): string

Returns a string body of the response. Returns null if the response has no body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  if (client.hasBody(response)) then
  begin
    var body:=client.getStringBody(response);
    ...
  end
end

Parameters :

response: the response.

Return :

the string body of the response.

Can be used in: 

hasBody

function hasBody(response: http_response): boolean

Returns true if the response has a body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  var response := client.get(request);
  if (client.hasBody(response)) then
  begin
    ...
  end
end

Parameters :

response: the response.

Return :

true if the response body or else false.

Can be used in: 

post

function post(request: http_request, body: string): http_response

Sends a POST request by setting the given body and handle the response. Raises an error if an I/O error occurs or the request times out. The returned response has status, headers and maybe a body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book');
  var body:='{  "content": {  "id": "B5253",
  "title":  "Da Vinci Code"
  }
  }';
  var response := client.post(request,body);
  ...
end

Parameters :

request: the request.

request: the body.

Return :

the received response.

Can be used in: 

put

function put(request: http_request, body: string): http_response

Sends a PUT request by setting the given body and handle the response. Raises an error if an I/O error occurs or the request times out. The returned response has status, headers and maybe a body.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.addPath(request,'book');
  client.addPath(request,'B5253');
  var body:='{  "content": {
  "author":  "Dan Brown"
  }
  }';
  var response := client.put(request,body);
  ...
end

Parameters :

request: the request.

body: the body.

Return :

the received response.

Can be used in: 

setHeader

procedure setHeader(request: http_request, headerField: string, value: string)

Sets a value to the header field of the request.

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.setHeader(request,'Content-Type','application/json');
  ...
end

Parameters :

request: the request.

headerField: the header field.

value: the value.

Can be used in: 

setTimeout

procedure setTimeout(request: http_request,timeout: int)

Sets a timeout to the request in milliseconds. It must be less than the default value defined in ebx properties file otherwise it is ignored. The default value of the timeout is different according to the context (trigger or workflow).

Example:

uses core.http as client;
...
begin
  var request := client.createRequest('ebx-1');
  client.setTimeout(request,900);
  ...
end

Parameters :

request: the request.

timeout: the timeout.

Can be used in: 

Documentation > Developer Guide > EBX® Script > API