Formdata with Axios in Node.js

Asked

Viewed 1,567 times

1

When I try to make a request using form-data with Node, the Springboot application that receives the request says it doesn’t have "Boundary". And when I put any value in "Boundary", the application does not recognize the data within form-data.

Follows the code:

const Formdata = require('form-data'); const Fs = require('Fs'); const api = require('.. /services/api')

module.Exports = {

async assina(req, res) {

    const { signatario, algoritmoHash, perfil } = req.body;
    const { fw_credencial, kms_credencial, kms_credencial_tipo, 
    auth_token } = req.headers;
    const { path } = req.file;

    const dadosAssinatura = {
        "signatario": signatario,
        "algoritmoHash": algoritmoHash,
        "perfil": perfil
    }


    console.log(req.file)

    const data = new FormData();

    const contents = fs.readFileSync(path);

    console.log(typeof(contents));

    console.log(JSON.stringify(dadosAssinatura));
    console.log(data)



    try {
        const response = await api.post('/kms/assinaturas', data, {
            headers: { 
                "kms_credencial": kms_credencial,
                "kms_credencial_tipo": kms_credencial_tipo,
                "auth_token": auth_token,
                "fw_credencial": fw_credencial,
                "Content-Type": "multipart/form-data;boundary=ronaldo" 
        }
        })

        console.log("DEU CERTO");
        return res.json(response.data);
    } catch (err) {
        console.log("erro");
    }

    return res.json(req.body.signatario)

}

}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.