OScript API/Built-in Package Index

Class: RestClient

The built-in functions in the RestClient class provide the ability to connect to a HTTP server and send get or post with content requests.

The RestClient class offers the following functionality:

Cookie Handling:

The RestClient has special handling for cookies. This applies to the functions: Get(), Post(), PostFile(), Delete(), Put(), PutFile(), Patch()

In order to send one or more cookies, a special Assoc of 'cookie name' - 'cookie value' pairs can be used. This special Assoc should be added to the headersFields Assoc parameter under a key named 'cookies'.

eg:

Assoc cookiesAssoc = { 'LLCookie' : 'asbvigh...vbirghbqit', 'mycookie' : 'sdv139...874to8qv' }
Assoc headersFields = Assoc{ 'cookies' : cookiesAssoc }

When the RestClient receives multiple cookies from the server, they are returned in a similar Assoc with 'cookie name' - 'cookie value' pairs under the key 'cookies' in the return Assoc from the function. They are not part of the responseHeaders Assoc. The 'cookies' Assoc is a sibling to the responseHeaders Assoc.

Instance Attributes

The name of the server

The URI

The port to connect to the server

The request timeout in seconds (default 200)

Class Methods

New( String hostName, Integer port, String initialURI, Integer timeout )

Returns a new RestClient instance, or undefined if an error occured.

NewSecure( String hostName, Integer port, String initialURI, [Integer timeout] )

Returns a new RestClient instance for HTTPS, or undefined if an error occured.

NewSecureWithCerts( String hostName, Integer port, String initialURI, [Integer timeout], [String certPath], [String certFile], [String keyFile] )

Returns a new RestClient instance for HTTPS, or undefined if an error occured.

Instance Methods

AddAuthentication( String userName, String password )

Adds user and password/cert authentication.

Delete( Assoc urlParams, [Assoc headerFields] )

Sends an HTTP delete.

Get( Assoc urlParams, [Assoc headerFields] )

Sends an HTTP get.

GetURI( )

Get the current URI.

Patch( Assoc urlParams, String body, Integer bodyLen, [Assoc headerFields] )

Sends an HTTP patch.

Post( Assoc urlParams, String body, Integer bodyLen, [Assoc headerFields], [Assoc postParams] )

Sends an HTTP post.

PostFile( Assoc urlParams, String filePath, String fileName, String postFileParam, String contentType, [Assoc headerFields], [Assoc postParams] )

Sends an HTTP post with local file reference to upload with the request.

Put( Assoc urlParams, String body, Integer bodyLen, [Assoc headerFields], [Assoc putParams] )

Sends an HTTP put.

PutFile( Assoc urlParams, String filePath, String fileName, String putFileParam, String contentType, [Assoc headerFields], [Assoc putParams] )

Sends an HTTP put with local file reference to upload with the request.

SetContentType( String contentType )

Set the Content-Type HTTP header.

SetDestFile( String filename )

Set the filename where content is written.

SetHeaderLogging( Boolean bLog )

Set logging of headers at debug level. Headers are not logged by default.

SetProxy( String hostName, Integer port )

Defines the host/port for a proxy server.

SetProxyAuth( String userName, String password )

Set the user and password for a proxy server.

SetURI( String newURI )

Set the URI.

Instance Attributes

String hostName

The name of the server

String initialURI

The URI

Integer nPort

The port to connect to the server

Integer nTimeout

The request timeout in seconds (default 200)

Class Methods

New

RestClient New( String hostName,
                Integer port,
                String initialURI,
                Integer timeout )

Returns a new RestClient object.

Parameters

hostName

IP address or domain name of HTTP host.

port

HTTP connection port, required

initialURI

The base URI

timeout

Request timeout, default 200, min value 15

Returns:

A new RestClient object or undefined if there was an error constructing the object.

NewSecure

RestClient NewSecure( String hostName,
                      Integer port,
                      String initialURI,
                      [Integer timeout] )

Returns a new RestClient object. Client verification of target server certificate is performed with system certificate store. Legacy parameters 5 through 7 ignored, see NewSecureWithCerts.

Parameters

hostName

IP address or domain name of HTTP host.

port

HTTP connection port, required

initialURI

The base URI

timeout

Request timeout, default 200, min value 15

Returns:

A new RestClient object or undefined if there was an error constructing the object.

NewSecureWithCerts

RestClient NewSecureWithCerts( String hostName,
                               Integer port,
                               String initialURI,
                               [Integer timeout],
                               [String certPath],
                               [String certFile],
                               [String keyFile] )

Returns a new RestClient object. Allows alternate client certificate store to be specified. On Linux, certPath parameter is required for target server certificate validation.

Parameters

hostName

IP address or domain name of HTTP host.

port

HTTP connection port, required

initialURI

The base URI

timeout

Request timeout, default 200, min value 15

certPath

Linux only. The path to default keystore, usually /etc/ssl/certs. Pass an empty string to enable target server verification with system certificate store. If not specified, verification of target server certificate disabled. On Windows, root certificates are ALWAYS loaded from the system store and used for verification of target server certificate.

certFile

Used only for client certificate authentication by target server. The path of the client certificate file. This file must contain a complete certificate chain and be encoded in PEM format.

keyFile

Used only for client certificate authentication by target server. The path of the client private key file. If this file contains both the key and certificate, then certFile parameter is not required.

Returns:

A new RestClient object or undefined if there was an error constructing the object.

Instance Methods

AddAuthentication

Integer AddAuthentication( String userName,
                           String password )

Adds authentication to the request and sends it if the server returns an authentication required http response.

Parameters

userName

The username for authentication

password

The password or certificate for authentication

Returns:

0 if successful or undefined if an error

Delete

Assoc Delete( Assoc urlParams,
              [Assoc headerFields] )

Constructs the header using the initialURI and supplied list of parm=value pairs and sends a delete.

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

Returns:

Request return or error with message

Get

Assoc Get( Assoc urlParams,
           [Assoc headerFields] )

Constructs the header using the initialURI and supplied Assoc of parm=value pairs and sends a get.

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

Returns:

Request return or error with message

GetURI

String GetURI()

Returns the current URI.

Returns:

The URI or undefined

Patch

Assoc Patch( Assoc urlParams,
             String body,
             Integer bodyLen,
             [Assoc headerFields] )

Constructs the header using the initialURI and supplied Assoc of parm=value pairs and sends a patch.

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

body

String containing patch content, may contain binary data

bodyLen

Length of the body content

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

Returns:

Request return or error with message

Post

Assoc Post( Assoc urlParams,
            String body,
            Integer bodyLen,
            [Assoc headerFields],
            [Assoc postParams] )

Constructs the header using the initialURI and supplied Assoc of parm=value pairs and sends a post.

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

body

String containing post content, may contain binary data

bodyLen

Length of the body content

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

postParams

Assoc of key value pairs to send as HTTP POST parameters. NOTE: Should NOT be URL escaped. This can only be specified if the body is empty and length is 0.

Returns:

Request return or error with message

PostFile

Assoc PostFile( Assoc urlParams,
                String filePath,
                String fileName,
                String postFileParam,
                String contentType,
                [Assoc headerFields],
                [Assoc postParams] )

Sends the given file as a HTTP multipart message

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

filePath

The fully qualified path to the local file that is to be uploaded

fileName

The file name to be used for the HTML form element (may be different than the local filename)

postFileParam

The HTML form element name associated with the file

contentType

The content or MIME type of the file

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

postParams

Assoc of key value pairs to send as HTTP multipart POST parameters. NOTE: Should NOT be URL escaped.

Returns:

Request return or error with message

Put

Assoc Put( Assoc urlParams,
           String body,
           Integer bodyLen,
           [Assoc headerFields],
           [Assoc putParams] )

Constructs the header using the initialURI and supplied Assoc of parm=value pairs and sends a put.

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

body

String containing put content, may contain binary data

bodyLen

Length of the body content

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

putParams

Assoc of key value pairs to send as HTTP multipart PUT parameters. NOTE: Should NOT be URL escaped.

Returns:

Request return or error with message

PutFile

Assoc PutFile( Assoc urlParams,
               String filePath,
               String fileName,
               String putFileParam,
               String contentType,
               [Assoc headerFields],
               [Assoc putParams] )

Sends the given file as a HTTP multipart message

Parameters

urlParams

Assoc of key value pairs to add to the URL query string (will URL encode if required)

filePath

The fully qualified path to the local file that is to be uploaded

fileName

The file name to be used for the HTML form element (may be different than the local filename)

putFileParam

The HTML form element name associated with the file

contentType

The content or MIME type of the file

headerFields

Assoc of key value pairs to add additional fields to the HTTP header

putParams

Assoc of key value pairs to send as HTTP multipart PUT parameters. NOTE: Should NOT be URL escaped.

Returns:

Request return or error with message

SetContentType

Integer SetContentType( String contentType )

Set the Content-Type HTTP header for all requests sent by this client.

Parameters

contentType

The value of the header

Returns:

0 or undefined

SetDestFile

Integer SetDestFile( String filename )

Allows larger content from the server to be written to a file instead of returning it in the Assoc.

Parameters

filename

String containing the file path/name

Returns:

0 or undefined

SetHeaderLogging

Integer SetHeaderLogging( Boolean bLog )

Enable/disable logging of headers at debug level. Note: passwords will be visible.

Parameters

bLog

Log headers on/off

Returns:

0 or undefined

SetProxy

Integer SetProxy( String hostName,
                  Integer port )

If set, the connection will be routed through the specified proxy server.

Parameters

hostName

IP address or domain name of proxy host.

port

Proxy connection port, required

Returns:

0 if successful or undefined if an error occurred.

SetProxyAuth

Integer SetProxyAuth( String userName,
                      String password )

If a proxy server is specified, this is the authentication for the proxy server.

Parameters

userName

The username for authentication

password

The passwordfor authentication

Returns:

0 if successful or undefined if an error

SetURI

Integer SetURI( String newURI )

Sets the URI to a new value.

Parameters

newURI

The URI

Returns:

0 or undefined

 Copyright © 2022 OpenText Corporation. All rights reserved.