Making HTTP Requests#
While creating nodes it is very commonn to call external APIs or make HTTP requests to other services.
This plays a major role during node development, maintenance, and improvements.
We provide a very flexible helper for making HTTP requests that abstracts away most of the complexity with a simple to use interface.
How to use#
In the node code, inside the execute
function you can easily call:
1 |
|
Where options
is an object in this format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Where url
is the only mandatory field. The default method is GET
.
Some notes about the possible fields:
- body: You can use a regular Javascript Object for JSON payload, a Buffer for file uploads, an instance of FormData for
multipart/form-data
andURLSearchParams
forapplication/x-www-form-urlencoded
. - headers: A simple key-value pair.
- If
body
is an instance ofFormData
thencontent-type: multipart/form-data
is injected automatically. - If
body
is an instance ofURLSearchParams
, thencontent-type: application/x-www-form-urlencoded
is added. - To override this behavior, you can set any
content-type
header you wish and it won't be overridden.
- If
- arrayFormat: If your query string contains an array of data, let's say
const qs = {IDs: [15,17]}
, the values set toarrayFormat
define how it will be sent.indices
(default):{ a: ['b', 'c'] }
will be formatted asa[0]=b&a[1]=c
brackets
:{ a: ['b', 'c'] }
will be formatted asa[]=b&a[]=c
repeat
:{ a: ['b', 'c'] }
will be formatted asa=b&a=c
comma
:{ a: ['b', 'c'] }
will be formatted asa=b,c
- auth: Used for Basic auth. Provide
username
andpassword
. - disableFollowRedirect: By default, we'll follow redirects. You can set this to false to prevent this from happening
- skipSslCertificateValidation: Used for calling HTTPS services without proper certificate
- returnFullResponse: Instead of returning only the body, returns an object with more data in the following format:
{body: body, headers: object, statusCode: 200, statusMessage: 'OK'}
- encoding: We usually detect the content type correctly but you can specify
arrayBuffer
to receive a Buffer you can read from and interact with.
Deprecation of the previous helper#
The previous helper implementation using this.helpers.request(options)
used and exposed the request-promise
library which was deprecated.
In an effort to keep maximum compatibility, we made a transparent conversion to another library called axios
.
If you are having issues, please report them in our Community Forums or on Github.
Also, you can temporarily enable Doc² to use the deprecated library by setting the environment variable WF²_USE_DEPRECATED_REQUEST_LIB=true
.
Please note: This behavior is permanent and we will be removing the request-promise
library entirely in the future so please report any errors you have so we can fix them.
Migration guide to the new helper#
As mentioned above, the previous helper is deprecated and will be replaced in the future. The new helper is much more robust, library agnostic, and easier to use.
New nodes should all use the new helper, and if you have built custom nodes we strongly suggest you migrate to the new helper. Here are the main considerations when migrating:
- Only
url
is accepted. Previouslyuri
was also accepted encoding: null
now must beencoding: arrayBuffer
rejectUnauthorized: false
is nowskipSslCertificateValidation: true
- Use
body
according tocontent-type
headers to clarify what is being sent resolveWithFullResponse
is nowreturnFullResponse
and has similar behavior
Example#
For an example, please check the Mattermost node.