如何解决Imgur上传api错误总是返回400,multer和form-data nodejs
我正在使用 react antd 上传图片。这是我处理上传请求的快递:
router.post('/upload-image',upload.any(),async function(req,res,next) {
const file = req.files[0];
const form = new FormData();
form.append('image',file.buffer);
form.append('type','file');
const imgurInstance = new imgur({
refreshToken: user.imgurRefreshToken,clientId: IMGUR_CLIENT_ID,clientSecret: IMGUR_CLIENT_SECRET
});
await imgurInstance.getAccessToken();
const response = await imgurInstance.uploadImage(form);
const data = await response.json();
});
这是记录的 req.files[0] 控制台:
{
fieldname: 'file',originalname: 'headscratch.jpeg',encoding: '7bit',mimetype: 'image/jpeg',buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 01 00 01 00 00 ff e1 00 42 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 01 87 69 00 04 00 00 00 01 00 00 ... 13803 more bytes>,size: 13853
}
这是我使用 node-fetch 的 uploadImage 函数
uploadImage (form){
const path = `/3/upload`;
const headers = {
'Authorization': `Bearer ${this.accessToken}`,...form.getHeaders()
};
const options = {
method: 'POST',body: form,headers
};
return fetch(`${this.BASE_API_URL}${path}`,options);
}
我总是从 imgur 得到 400,但他们没有提供任何详细信息。
{
status: 400,success: false,data: { error: 'Bad Request',request: '/3/upload',method: 'POST' }
}
我尝试过使用 base64,只是使用 form-data 上传了一个 url,但我仍然收到 400 错误。有没有人对如何成功地使这个电话工作有任何指导?谢谢。
解决方法
我想我需要了解表单数据实际上在做什么。显然表单数据不会作为文件发送,除非您将文件名与 {filename: ''} 作为一个选项
form.append('image',file.buffer,{filename: file.originalname});
imgur 终于接受了我的请求,瞧。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。