REST APIs
Call REST APIs with basic, token or custom authentication
- REST API with Basic Authentication
- REST API with Token Authentication
- REST API with Secured Token Authentication
- REST API with Custom Headers
- Certificates and timeouts
REST API with Basic Authentication
Call a REST API with the user and password of a stored credential:
fn.fnRestBasic(
'get',
'https://resturl/api/',
'',
'name_of_credential_in_database',
'[.records[] | {name:.name, email:.email, spouse:.relations.spouse}]',
'name',
false
)
// output : a full json object coming from rest, json transformed with jq, result sorted and transformed by javascript.
// fn.fnRestBasic(method,url,body,credentialname,jq-expression,sort-object)
// - method : get,post,put,delete
// - url : url to restapi (can for example contain a placeholder like 'https://$(serverfield.fqdn)/api/'
// - body : in case of post and put
// - credential-name : it will lookup the credentials as you have saved in the gui
// - jq-expression : an optional jq-expression (https://jqplay.org)
// - sort object : a sort object to order the result
// - hasBigInt : a boolean indicating if it should convert Int64 to string
REST API with Token Authentication
Call a REST API with a bearer token written in the expression:
fn.fnRestJwt(
'get',
'https://resturl/api/',
'',
'your_jwt_token_in_text',
'[.records[] | {name:.name, age:.age}]',
['age','name'],
false
)
// output : a full json object coming from rest, json transformed with jq, result sorted, first by age, then by name
// fn.fnRestJwt(method,url,body,token,jq-expression,sort-object)
// - method : get,post,put,delete
// - url : url to restapi (can for example contain a placeholder like 'https://$(serverfield.fqdn)/api/'
// - body : in case of post and put
// - token : it will be add as a Bearer Authorization header
// - jq-expression : an optional jq-expression (https://jqplay.org)
// - sort-object : a sort object to sort the result
// - hasBigInt : a boolean indicating if it should convert Int64 to string
// - tokenPrefix : a prefix, defaults to 'Bearer' (v5.0.0)
REST API with Secured Token Authentication
Call a REST API with a bearer token taken from a stored credential, so it never appears in the form:
fn.fnRestJwtSecure(
'get',
'https://resturl/api/',
'',
'name_of_credential_in_database',
'[.records[] | {name:.name, age:.age}]',
['age','name'],
false
)
// output : a full json object coming from rest, json transformed with jq, result sorted, first by age, then by name
// fn.fnRestJwtSecure(method,url,body,token,jq-expression,sort-object)
// - method : get,post,put,delete
// - url : url to restapi (can for example contain a placeholder like 'https://$(serverfield.fqdn)/api/'
// - body : in case of post and put
// - credential_name : Encrypted credential will be looked up. The password will be the token.
// - jq-expression : an optional jq-expression (https://jqplay.org)
// - sort-object : a sort object to sort the result
// - hasBigInt : a boolean indicating if it should convert Int64 to string
// - tokenPrefix : a prefix, defaults to 'Bearer' (v5.0.0)
REST API with Custom Headers
Call a REST API with headers of your own, for any other kind of authentication:
fn.fnRestAdvanced(
'get',
'https://resturl/api/',
'',
{'a_custom_http_header':'your_value','Authorization':'basic base64(my_rest_credential)'},
'.records[].name',
{name:{ignoreCase:true,direction:'desc'}},
false,
false
)
// output : a full json object coming from rest, json transformed with jq, result sorted descending by name
// fn.fnRestAdvanced(method,url,body,{myheader:'value'},jq-expression,sort-object)
// - method : get,post,put,delete
// - url : url to restapi (can for example contain a placeholder like 'https://$(serverfield.fqdn)/api/'
// - body : in case of post and put
// - headers: an object of headers
// - jq-expression : an optional jq-expression (https://jqplay.org)
// - sort-object : a sorting object to order the results
// - hasBigInt : a boolean indicating if it should convert Int64 to string
// - raw : return data and response_headers
// 3 function-placeholders are allowed in the headers:
// - base64(credential_name) : will create a base64 encoded "username:password". prefix with "basic" if required
// - username(credential_name) : will add the username of the credential
// - password(credential_name) : will add the password of the credential
Certificates and timeouts
From 7.3.0 every REST helper function (fnRestBasic, fnRestAdvanced, fnRestJwt, fnRestJwtSecure) checks the TLS certificate of the endpoint and stops waiting after a set time:
| Setting | What it changes |
|---|---|
REST_IGNORE_CERTS | 1 skips the certificate check. Up to 7.2.0 it was never checked; keep it at 0 and trust a private CA with NODE_EXTRA_CA_CERTS instead |
REST_TIMEOUT_SECONDS | How long a call waits for an answer, 60 seconds by default; an endpoint that never answers then fails the field instead of holding it |
From 7.3.0 a field whose endpoint has a self-signed certificate fails with a certificate error until one of the two is set. REST calls and jq describes both, and how to trust a private CA for every REST call at once. fn.fnParseHtmlWithRegex is not a REST helper: it ignores both settings, always checks the certificate and waits at most 30 seconds.