4.6 KiB
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:
const response = await this.helpers.httpRequest(options);
Where options is an object in this format:
{
url: string;
headers?: object;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
body?: FormData | Array | string | number | object | Buffer | URLSearchParams;
qs?: object;
arrayFormat?: 'indices' | 'brackets' | 'repeat' | 'comma';
auth?: {
username: string,
password: string,
};
disableFollowRedirect?: boolean;
encoding?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
skipSslCertificateValidation?: boolean;
returnFullResponse?: boolean;
proxy?: {
host: string;
port: string | number;
auth?: {
username: string;
password: string;
},
protocol?: string;
};
timeout?: number;
json?: boolean;
}
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-dataandURLSearchParamsforapplication/x-www-form-urlencoded. - headers: A simple key-value pair.
- If
bodyis an instance ofFormDatathencontent-type: multipart/form-datais injected automatically. - If
bodyis an instance ofURLSearchParams, thencontent-type: application/x-www-form-urlencodedis added. - To override this behavior, you can set any
content-typeheader 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 toarrayFormatdefine how it will be sent.indices(default):{ a: ['b', 'c'] }will be formatted asa[0]=b&a[1]=cbrackets:{ a: ['b', 'c'] }will be formatted asa[]=b&a[]=crepeat:{ a: ['b', 'c'] }will be formatted asa=b&a=ccomma:{ a: ['b', 'c'] }will be formatted asa=b,c
- auth: Used for Basic auth. Provide
usernameandpassword. - 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
arrayBufferto 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 n8n to use the deprecated library by setting the environment variable N8N_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
urlis accepted. Previouslyuriwas also accepted encoding: nullnow must beencoding: arrayBufferrejectUnauthorized: falseis nowskipSslCertificateValidation: true- Use
bodyaccording tocontent-typeheaders to clarify what is being sent resolveWithFullResponseis nowreturnFullResponseand has similar behavior
Example
For an example, please check the Mattermost node.