José Carlos
José Carlos

Reputation: 11

Multipart/form-data sends empty data using axios API Rest

Hello Team. I'm trying to make a request, however, it looks like my body isn't being sent.

I made an API that returns me exactly what I'm sending, however the body is returning empty.

Notes:

1 - Through an API, I need to send a file with .txt or .msg format.

2 - I have hidden some of the information below as this is sensitive business data.

const axios = require('axios');



async function UploadFile(urlNovaTarefa, siteUrl, clientId, clientSecret, nomeCentral, grupoId, AssuntoId, usuario, solicitante, copia, solicitacao, anexoNome, anexoConteudo, dataEntrada, enviarNotificacao, cnpjPedido, responsavel){
// formData
const form = new FormData();
form.append('NomeCentral', nomeCentral);
form.append('GrupoId', grupoId);
form.append('AssuntoId', AssuntoId);
form.append('Usuario', usuario);
form.append('Solicitante', solicitante);
form.append('Copia', copia);
form.append('Solicitacao', solicitacao);
form.append('AnexoNome', anexoNome);
form.append('AnexoConteudo', anexoConteudo);
form.append('DataEntrada', dataEntrada);
form.append('EnviarNotificacao', enviarNotificacao);
form.append('CNPJPedido', cnpjPedido);
form.append('Responsavel', responsavel);



//console.log(form);

let response = await axios.post(urlNovaTarefa, form, {
headers: {
'SiteUrl': siteUrl,
'Client-Id': clientId,
'Client-Secret': clientSecret,
'Content-Type': "multipart/form-data; boundary=CSC",
'Access-Control-Allow-Origin': "*",
},
});



let data = response.data;
console.log(data);
}




UploadFile("https://example.net/ct/csc/novaTarefa", "https://example.com/sites/centralcsc/", "1234-1234-1234-1234", "abcd-abcd-abcd-abcd=", "ServicosClientes", "4", "3", "[email protected]", "[email protected]", "[email protected]", "Test", "Test.txt", "C:\\Users\\User\\Desktop\\Test.txt", "07/10/2021 14:30", "N", "[{\"NumeroCNPJ\":\"12345678910112\",\"NumeroPedido\":[]}]", "[email protected]")
.then(response => console.log(response))
.catch(err => console.log(err))

What should be being sent on my body (form-data) is this:

'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="NomeCentral"\r\n' +
'\r\n',
'ServicosClientes',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="GrupoId"\r\n' +
'\r\n',
'4',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="AssuntoId"\r\n' +
'\r\n',
'3',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Usuario"\r\n' +
'\r\n',
'[email protected]',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Solicitante"\r\n' +
'\r\n',
'[email protected]',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Copia"\r\n' +
'\r\n',
'[email protected]',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Solicitacao"\r\n' +
'\r\n',
'Test',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="AnexoNome"\r\n' +
'\r\n',
'Test.txt',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="AnexoConteudo"\r\n' +
'\r\n',
'C:\\Users\\User\\Desktop\\Test.txt',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="DataEntrada"\r\n' +
'\r\n',
'07/10/2021 14:30',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="EnviarNotificacao"\r\n' +
'\r\n',
'N',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="CNPJPedido"\r\n' +
'\r\n',
'[{"NumeroCNPJ":"12345678910112","NumeroPedido":[]}]',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Responsavel"\r\n' +
'\r\n',
'[email protected]',
[Function: bound ]
]

This is the return of my API! The Body is empty.

Resultado: 0,
Mensagem: 'Success',
Headers: {
connection: 'Keep-Alive',
'transfer-encoding': 'chunked',
accept: 'application/json, text/plain, */*',
host: 'example.net',
'max-forwards': '10',
'user-agent': 'axios/0.23.0',
'x-client-ip': '000.00.00.000',
'x-client-port': '00000',
siteurl: 'https://example.com/sites/centralcsc/',
'client-id': '1234-1234-1234-1234',
'client-secret': 'abcd-abcd-abcd-abcd=',
'access-control-allow-origin': '*',
'x-waws-unencoded-url': '/ct/csc/novaTarefa',
'client-ip': '000.00.00.000:00000',
'x-arr-log-id': 'xxxxxxxxxxxxxxxxxxxx',
'disguised-host': 'example.net',
'x-site-deployment-id': 'example',
'was-default-hostname': 'example.net',
'x-original-url': '/ct/csc/novaTarefa',
'x-forwarded-for': '000.00.00.000:00000',
'x-arr-ssl': '2048|256|C=US, O=Microsoft Corporation, CN=Microsoft RSA TLS CA 02|CN=*.example.net',
'x-forwarded-proto': 'https',
'x-appservice-proto': 'https',
'x-forwarded-tlsversion': '1.2',
'request-id': '|999999999-99999999999999.1.',
'content-type': 'multipart/form-data; boundary=CSC'
},
Body: {}
}
undefined

Upvotes: 0

Views: 558

Answers (1)

José Carlos
José Carlos

Reputation: 11

I solved it. I just changed my POST call

  _headers["SiteUrl"] = siteUrl;
  _headers["Client-Id"] = clientId;
  _headers["Client-Secret"] = clientSecret;
  let response = await axios.post(urlNovaTarefa, form.getBuffer(), {
    headers: _headers,
  });```

Upvotes: 1

Related Questions