node使用form-data上传文件
node无法直接像html中使用new FormData() 创建对象,要使用form-data库,具体使用参考npm-from-data,使用举例如下:
const axios = require('axios');
const FormData = require('form-data');
const path = require('path');
const fs = require('fs');
const form = new FormData();
form.append('transactionType', '02');
form.append('transactionIDType', '02');
form.append('transactionID', '02202208030919595260');
form.append('files', fs.createReadStream(path.resolve(__dirname, './imgs/a.png')));
axios({
url: 'http://10.211.66.173:8123/ca/api/ndc/v0/api/attachment',
method: 'post',
data: form,
headers: {
...form.getHeaders(),
'Host': '10.211.66.173',
'Channel-ID': 'AIRCHINA_TCH'
}
}).then(
res => {
console.log('==>res', res.data)
}).catch(
err => {
console.log('err', err)
})