The AxiosHttp
connection is used to connect to APIs and web servers using HTTP or HTTPS.
It uses the axios library.
The same properties can be set on both connections and requests. The properties will be merged, with the request properties overwriting connection properties. This allows properties like authentication headers and the baseURL to be set on the connection, with request specific properties like the request.
Secrets like authentication headers and API keys should be stored using the
_secret
operator.
Connections
Connection types:
- AxiosHttp
Requests
Request types:
- AxiosHttp
AxiosHttp
Properties
url: string
: The server URL that will be used for the request.method: enum
: Default:'get'
- The request method to be used when making the request. Options are:'get'
'delete'
'head'
'options'
'post'
'put'
'patch'
baseURL: string
:baseURL
will be prepended tourl
unlessurl
is absolute. It can be convenient to setbaseURL
for an axios connection to pass relative URLs to requests or mutations using that connection.headers: object
: An object with custom headers to be sent with the request. The object keys should be header names, and the values should be the string header values.params: object
: An object with URL parameters to be sent with the request.data: string | object | array
: The data to be sent as the request body. Only applicable for request methods'put'
,'post'
, and'patch'
. Can be an object, array or a string in the format'Country=Brasil&City=Belo Horizonte'
.auth: object
: Indicates that HTTP Basic authorization should be used, and supplies credentials. This will set anAuthorization
header, overwriting any existingAuthorization
custom headers you have set usingheaders
. Only HTTP Basic auth is configurable through this parameter, for Bearer tokens and such, useAuthorization
custom headers instead. Theauth
object should have the following fields:username: string
password: string
httpAgentOptions: object
: Options to pass to the Node.jshttp.Agent
class.httpsAgentOptions: object
: Options to pass to the Node.jshttps.Agent
class.maxContentLength: number
: Defines the max size of the http response content allowed in bytes.maxRedirects: number
: Defines the maximum number of redirects to follow. If set to 0, no redirects will be followed.proxy: object
: Defines the hostname and port of the proxy server. Theproxy
object should have the following fields:host: string
: Host IP address (eg.'127.0.0.1'
).port: number
: Port number.auth: object
: Object with username and password.
responseType: enum
: Default:'json'
- The type of data that the server will respond with. Options are:'document'
'json'
'text'
responseEncoding: string
: Default:'utf8'
- Indicates encoding to use for decoding responses.timeout: number
: Default:0
(no timeout) - The number of milliseconds before the request times out. If the request takes longer thantimeout
, the request will be aborted. Set to0
for no timeout.transformRequest: function
: A function to transform the request data before it is sent. Receivesdata
andheaders
as arguments and should return the data to be sent.transformResponse: function
: A function to transform the received data. Receivesdata
as an argument and should return the transformed data.validateStatus: function
: A function to validate whether the response should complete successfully given a status code. Receivesstatus
as an argument and shouldtrue
if the status code is valid.
Examples
Properties for a AxiosHttp can be set on either the connection, the request, or both. However, both a connection and request need to be defined.
A basic full example requesting data from https://jsonplaceholder.typicode.com
lowdefy: 3.23.3
name: Lowdefy starter
connections:
- id: my_api
type: AxiosHttp
properties:
baseURL: https://jsonplaceholder.typicode.com
pages:
- id: welcome
type: PageHeaderMenu
requests:
- id: get_posts
type: AxiosHttp
connectionId: my_api
properties:
url: /posts
events:
onEnter:
- id: fetch_get_posts
type: Request
params: get_posts
blocks:
- id: rest_data
type: Markdown
properties:
content:
_string.concat:
- |
```yaml
- _yaml.stringify:
- _log:
_request: get_posts
- |
```
Using the connection to set baseURL and authorization, and handle specific requests as requests.
connections:
- id: app_api
type: AxiosHttp
properties:
baseURL: app.com/api
auth:
username: api_user
password:
_secret: API_PASSWORD
# ...
requests:
- id: get_orders
type: AxiosHttp
connectionId: app_api
properties:
url: /orders
- id: update_order
type: AxiosHttp
connectionId: app_api
properties:
url:
_nunjucks: /orders/{{ order_id }}
method: post
data:
_state: true
Setting properties on only the connection:
connections:
- id: get_count
type: AxiosHttp
properties:
url: myapp.com/api/count
headers:
X-Api-Key:
_secret: API_KEY
# ...
requests:
- id: get_count
type: AxiosHttp
connectionId: get_count
Setting properties on only the request, and using a generic connection:
connections:
- id: axios
type: AxiosHttp
# ...
requests:
- id: get_api_1
type: AxiosHttp
connectionId: axios
properties:
url: app1.com/api/things
- id: post_to_api_2
type: AxiosHttp
connectionId: axios
properties:
url: api.otherapp.org/other/thing
method: post
data:
_state: true