ToB企服应用市场:ToB评测及商务社交产业平台
标题:
node 通过axios发送post哀求(FormData)
[打印本页]
作者:
尚未崩坏
时间:
2024-9-16 21:32
标题:
node 通过axios发送post哀求(FormData)
方案一:
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const sdUpscaleOnAzure = async (req, res) => {
const data = new FormData()
data.append('image', fs.readFileSync('/temp/ai/sd/download/1.png'))
let config = {
headers: {
...data.getHeaders(),
Accept: 'application/json',
},
method: 'post',
}
config.url = 'https://www.baidu.com'
config.data = data
console.log(config)
axios({
...config,
})
.then(async (response) => {
res.send({
code: 200,
data: {
response: response.data,
},
message: '成功',
})
})
.catch((err) => {
res.send({
code: 400,
data: {
err,
},
message: '失败2',
})
})
}
复制代码
方案二:
const data = new FormData()
let tempData = config.data
for (let key in tempData) {
data.append(key, tempData[key])
}
config.data = data
config.headers['Content-Type'] =
'multipart/form-data; boundary=' + data.getBoundary()
axios({
...config,
})
.then((response) => {
res.send({
code: 200,
data: {
response: response.data,
},
message: '成功',
})
})
.catch((err) => {
res.send({
code: 400,
data: {
err,
},
message: '失败',
})
})
复制代码
参考链接:
https://chat.xutongbao.top/
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4