REST API

通过基本认证、令牌或自定义认证调用 REST API

  1. 使用基本认证的 REST API
  2. 使用令牌认证的 REST API
  3. 使用安全令牌认证的 REST API
  4. 使用自定义请求头的 REST API
  5. 证书和超时

使用基本认证的 REST API

使用已保存凭据的用户名和密码调用 REST API:

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

使用写在表达式中的 bearer 令牌调用 REST API:

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

使用从已保存凭据中获取的 bearer 令牌调用 REST API,这样令牌永远不会出现在表单中:

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

使用您自己的请求头调用 REST API,适用于其他任何认证方式:

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

证书和超时

从 7.3.0 起,每个 REST 辅助函数( fnRestBasic 、 fnRestAdvanced 、 fnRestJwt 、 fnRestJwtSecure )都会校验端点的 TLS 证书,并在设定时间后停止等待:

设置 作用
REST_IGNORE_CERTS 1 跳过证书校验。在 7.2.0 及之前从不校验证书;请保持为 0 ,改用 NODE_EXTRA_CA_CERTS 信任私有 CA
REST_TIMEOUT_SECONDS 调用等待响应的时长,默认 60 秒;端点一直不响应时,字段会失败,而不是一直挂起

从 7.3.0 起,如果字段的端点使用自签名证书,在设置这两项之一之前,该字段会因证书错误而失败。 REST 调用和 jq 介绍了这两项设置,以及如何一次性让所有 REST 调用信任私有 CA。 fn.fnParseHtmlWithRegex 不是 REST 辅助函数:它忽略这两项设置,始终校验 证书,最多等待 30 秒。