微信小程序上传文件400
直接上示例问题代码
wx.uploadFile({
url: 'https://' + domain + '/post/upload/cover/test',
filePath: tempFilePaths[i],
name: "multipartFile",
formData: {
type: "4"
},
header: {
"content-type": "multipart/form-data",
// "Host": domain
},
success: function (res) {
console.log(res)
// 先获取结果集的
// files/img-files/1/e2102802-6f16-45af-b926-9d0b4c2cc5d3/cover.png
// console.log(JSON.parse(res.data).data.data)
var strs = JSON.parse(res.data).data.data.split('/')
var uuid = strs[strs.length - 2]
console.log(uuid)
// wx.showToast({
// title: "上传成功",
// icon: "none",
// duration: 1500
// })
that.data.imgs.push(JSON.parse(res.data).data)
that.setData({
imgs: that.data.imgs
})
},
fail: function (err) {
wx.showToast({
title: "上传失败",
icon: "none",
duration: 2000
})
},
complete: function (result) {
console.log(result.errMsg)
}
})
如上所示 应该是没什么问题,请求地址使用localhost本地端口也没问题。
但是使用花生壳内网穿透后,用花生壳给的域名就出现400问题,尝试通过花生壳访问swagger也没问题。
对比了请求信息,发现请求头的参数很多没有,而swagger有host,Origin等请求头,应该就是请求头缺乏的问题,尝试添加了Host请求头后, 成功上传了。
将请求头添加上Host即可解决
Host就是主机的意思,比如url是 https://abc.def.com/uploadfile 的话,domain就是 abc.def.com