doo.functions.request

How to call HTTP request from Tabidoo.

Sometimes it is necessary to send data to external systems after specific actions in Tabidoo (eg after data was saved). For example, you may want to send a Slack notification after data was saved with a specific value in a specific field - you can send a message using an HTTP request to the Slack API. Or you may want to save data to the external CRM/ERP system after data was saved in Tabidoo - you can send the data using an HTTP request to the API of any external system if this system has API.

In Tabidoo JavaScript, you can perform an HTTP request to any API or server you want - both Tabidoo API and any other API on other servers.

An HTTP request can be performed on the client-side in JavaScript of the edit form (events On model load, On model change, Before model save, and After model save) and on the server-side (events Before model save and After model save).

An HTTP request cannot be performed in JavaScript for calculated fields, JavaScript for a data load condition defined in a user role, and in JavaScript for a condition for using a whole user role.

You can use one of the functions in the doo.functions.request namespace to make an HTTP request:

async get(url[, options])
perform GET HTTP request

async post(url, body[, options])
perform POST HTTP request

async custom(url, options)
perform any HTTP request - GET/POST/PATCH/HEAD...

All functions are asynchronous, and you can use the await keyword to wait and get the result of an HTTP request.

An HTTP request can have some options depending on whether it is performed on the client-side or on the server-side. All options are optional and have a default value if they are not provided.

OPTIONS

{
    // These properties are part of the Fetch Standard
    method: 'GET',
    headers: null,
    body: null,
    redirect: 'follow',
    signal: null,
 
    // The following properties are Tabidoo-fetch extensions
    params: null,
    basicAuth: null, 
    bearer: null,
    proxy: null,
    mode: 'cors'                  // client/browser-side JavaScript only
    allowInvalidSSLCert: false    // server-side JavaScript only
}

OPTIONS - description

method
The request method, e.g. GET/POST/PUT/PATCH/DELETE/OPTIONS/HEAD/...
It is used in CUSTOM function only.
Default 'GET'.

headers
Any headers you want to add to your request - key-value dictionary (object), e.g.
Default null.

{
    'my-header': 'abc',
    'my-header2': '123',
}

body
Any body that you want to add to your request: this can be a Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object. Note that a request using the GET or HEAD method cannot have a body.
Default null.

redirect
The redirect mode to use: follow (automatically follow redirects), error (abort with an error if a redirect occurs), or manual (handle redirects manually).
Default 'follow'.

signal
An AbortSignal object instance; allows you to communicate with a fetch request and abort it if desired via an AbortController. Can be used for cancelation request after some timeout.
Default null.

params
Query parameters for URL query string - key-value dictionary (object), e.g.
https://url?name=John&surname=Smith
Default null.

{ 
    name: 'John',
    surname: 'Smith',
}

basicAuth
Username and password for basic authorization header (object), e.g.
Default null.

{
    username: 'abc',
    password: '***'
}

bearer
Bearer token for authorization header (string).
Default null.

proxy
Proxy address, e.g. https://1.2.3.4:3128
Default null.

mode (client/browser-side only)
The mode you want to use for the request, e.g., cors, no-cors, or same-origin.
Default cors.

cache (client/browser-side only)
It controls how the request will interact with the browser's HTTP cache.
Values default , no-store, reload, no-cache, force-cache, only-if-cached.
Default 'default'.

allowInvalidSSLCert (server-side only)
Allow invalid target SSL certificate for HTTPS request - value true/false.
Default false.

Examples

GET example

const res = await doo.functions.request.get('https://myserver.com:1234/info');
const res = await doo.functions.request.get('https://myserver.com/info', 
    {
        basicAuth: { username: 'abc', password: '123'} 
    });

POST example

await doo.functions.request.post('https://hooks.slack.com/services/123/abc/987zyx', 
    {
        'text': 'Slack Message - Employee's surname: ' + doo.model.surname.value
    }, 
    { 
        mode: 'no-cors' 
    });

CUSTOM example

await doo.functions.request.custom('https://myserver.com/api/employee', 
    { 
        body: { 
                  surname: 'New Name'
              }, 
        bearer: '123abc', 
        allowInvalidSSLCert: true 
    });