Skip to content
background-image background-image

Using node-fetch (form data)

This example shows how to use node-fetch to send form data, upload and download files

Send form

// submit form data to external service
const body = new FormData();
body.append('username', 'abc');
const resp = await fetch('https://httpbin.org/post', { method: 'POST', body });
const data = await resp.text();
return [];

Download file

// download raw file and convert it to base64
const resp = await fetch('https://www.google.com/favicon.ico');
const data = await resp.arrayBuffer();
const base64 = Buffer.from(data).toString('base64');

return [];

Upload Base64 content as file

const base64 = 'SGVsbG8gV29ybGQ='; // Hello World
// upload base64 content
const buffer = Buffer.from(base64, 'base64');
const body = new FormData();
body.append('file', new File([buffer], 'favicon.ico', { type: 'image/x-icon' }));
const resp = await fetch('https://httpbin.org/post', { method: 'POST', body });
const data = await resp.text();

return [];