Skip to content

Functions:HTTP

Emiel Wit edited this page Jun 1, 2023 · 22 revisions

HTTP

As functions are run from a sandboxed environment, it's not possible to use libraries or functions that try to connect to other services over HTTP(S). Instead we supply a wrapper function, fetch, around node-fetch which lets you call those services over HTTP(S). For documentation on how to use this function, it's best to look at the documentation of node-fetch.


fetch

Example:

constresponse=awaitfetch('https://example.com');consttext=awaitresponse.text();

Form data

To send form data with the fetch helper, the fetch helper accepts an optional argument formData which is an array of objects key, type (optional), value, fileName (optional). To send a file in the form data the value has to be an ArrayBuffer and there should be passed a type property with value buffer. If this structure is being received by the fetch helper, it will parse it to an actual file. If there is no type property inside the object, the value remains as it is. The optional fileName will be passed as the third argument in the form data body to support certain requests with files in the FormData.

Example:

constresponse=awaitfetch('https://example.com/file.pdf');constblob=awaitresponse.blob();constbuffer=blob.buffer;constrequest={method: 'POST',headers: {'X-Auth-Token': 'testKey'},formData: [{key: 'text',value: 'Example text'},{key: 'secondFile',type: 'buffer',value: buffer,fileName: 'file.pdf'},],};constresponse=awaitfetch('https://example.com',request);

Proxy agent

It's possible to use a proxy agent click here for more information and see the example below.

Example:

constrequest={method: 'POST',proxyAgent: {host: "host.io",port: 80,auth: "username:password",},};constresponse=awaitfetch('https://example.com',request);

HTTP(s) agent

HTTP(s) agents can be used by passing the options to the helper as shown in the example below. More information about the available options can be found here.

Example:

constrequest={method: 'POST',httpsAgent: {keepAlive: true}};constresponse=awaitfetch('https://example.com',request);

Clone this wiki locally