[英]Bad request status 400 when trying to upload image file to imgur
I'm trying upload image file from my React app.我正在尝试从我的 React 应用程序上传图像文件。 Keep getting 400 error, witch I can't understand why.不断收到 400 错误,我不明白为什么。 That's my TypeScript function's code in React:这是我在 React 中的 TypeScript 函数代码:
public setImage = (args: ChangeEvent<HTMLInputElement>) => {
const image = args.target.files[0];
let formData: FormData = new FormData();
formData.append("profileImage", image, image.name);
const requestOptions = {
method: 'POST',
headers: { 'Authorization': 'Client-ID //my client-id here//' },
image: image
};
fetch('https://api.imgur.com/3/image', requestOptions)
.then(response => response.json())
.then(data => console.log(data));
}
And the error:和错误:
{
data: error: "No image data was sent to the upload api"
method: "POST"request: "/3/image"
__proto__: Objectstatus: 400
success: false
__proto__: Object
}
When i'm trying to console my FormData object after appending I can see an empty object.当我在追加后尝试控制台我的 FormData 对象时,我可以看到一个空对象。 Thanks for any answer!感谢您的任何回答!
The first thing that comes to mind is to log the image object as well and see if there are any problems there.首先想到的是记录图像对象,看看那里是否有任何问题。
Also please take a look at this to verify that you're logging your FormData object correctly.另请查看此内容以验证您是否正确记录了 FormData 对象。
If however, this is still not enough to debug the issue, I would advise you to try rolling a request using something like curl or Fiddler since you are manually creating the POST headers.但是,如果这仍然不足以调试问题,我建议您尝试使用 curl 或 Fiddler 之类的东西来滚动请求,因为您正在手动创建 POST 标头。 Because of this, you're not sending the Content-Length header which could cause the imgur api to just skip the whole body of your request.因此,您不会发送 Content-Length 标头,这可能会导致 imgur api 跳过整个请求正文。
To get the content lenght you could do something like:要获得内容长度,您可以执行以下操作:
let content-length = JSON.stringify(formData).length;
Then simply:然后简单地:
const requestOptions = {
method: 'POST',
headers: { 'Authorization': 'Client-ID //my client-id here//', 'Content-Length': content-length },
image: image
};
Hope this is useful in some way.希望这在某种程度上有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.