Skip to content

Instantly share code, notes, and snippets.

@romain130492
Last active April 7, 2020 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romain130492/4c7e02ebd07b7615ec9975f002b0ebb9 to your computer and use it in GitHub Desktop.
Save romain130492/4c7e02ebd07b7615ec9975f002b0ebb9 to your computer and use it in GitHub Desktop.
#Axios - Async #Javascript Post request (Multi Form Data) with NodeJs
  • An async Post request (Multi Form Data) with NodeJs
    let formData = new FormData();
    formData.append('input_file', fs.createReadStream('your file path'));
    formData.append('parameter_1', 'your parameter');
    const getData = async url => {
        try {
            const res = await axios.post(url, formData, {
                // You need to use `getHeaders()` in Node.js because Axios doesn't
                // automatically set the multipart form boundary in Node.
                headers: formData.getHeaders()
            });
            const data = res.data;
            console.log(data, 'Data')
        } catch (error) {
            console.log(error.response.data.errors, 'Error')
        }
    }

    getData(url, formData);
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment