Functions:HTTP - bettyblocks/cli GitHub Wiki
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:
const response = await fetch('https://example.com');
const text = await response.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:
const response = await fetch('https://example.com/file.pdf');
const blob = await response.blob();
const buffer = blob.buffer;
const request = {
method: 'POST',
headers: { 'X-Auth-Token': 'testKey' },
formData: [
{ key: 'text', value: 'Example text' },
{ key: 'secondFile', type: 'buffer', value: buffer, fileName: 'file.pdf' },
],
};
const response = await fetch('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:
const request = {
method: 'POST',
proxyAgent: {
host: "host.io",
port: 80,
auth: "username:password",
},
};
const response = await fetch('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:
const request = {
method: 'POST',
httpsAgent: {
keepAlive: true
}
};
const response = await fetch('https://example.com', request);