【问题标题】:Multipart/form-data sends empty data using axios API RestMultipart/form-data 使用 axios API Rest 发送空数据
【发布时间】:2021-12-09 10:13:57
【问题描述】:

你好团队。 我正在尝试提出请求,但是,我的身体似乎没有被发送。

我创建了一个 API,它返回的正是我发送的内容,但正文返回的是空的。

注意事项:

1 - 通过 API,我需要发送 .txt 或 .msg 格式的文件。

2 - 我隐藏了下面的一些信息,因为这是敏感的业务数据。

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@example.com", "email@example.com", "email@example.com", "Test", "Test.txt", "C:\\Users\\User\\Desktop\\Test.txt", "07/10/2021 14:30", "N", "[{\"NumeroCNPJ\":\"12345678910112\",\"NumeroPedido\":[]}]", "email@example.com")
.then(response => console.log(response))
.catch(err => console.log(err))

应该在我的身体上发送的内容(表单数据)是这样的:

'----------------------------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@example.com',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Solicitante"\r\n' +
'\r\n',
'email@example.com',
[Function: bound ],
'----------------------------553386948041586350665922\r\n' +
'Content-Disposition: form-data; name="Copia"\r\n' +
'\r\n',
'email@example.com',
[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@example.com',
[Function: bound ]
]

这是我的API的回报! 身体是空的。

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

【问题讨论】:

    标签: node.js api axios multipartform-data


    【解决方案1】:

    我解决了。我刚刚更改了我的 POST 调用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-09
      • 2012-10-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多