简体   繁体   English

尝试将图像文件上传到 imgur 时出现错误的请求状态 400

[英]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.

相关问题 尝试使用 MERN 上传图像时出现 400 错误请求错误 - Trying to upload an image using MERN getting 400 bad request error 尝试上传到AWS S3存储桶时收到400错误请求 - getting 400 Bad Request when trying to upload to aws s3 bucket 在reactjs中使用axios发出请求时的400(错误请求)状态码 - 400(Bad request) status code when making request with axios in reactjs 使用 React 将 blob 文件上传到 cloudinary,Cloudinary Bad Request 400 - Upload blob file to cloudinary using React, Cloudinary Bad Request 400 在React中尝试对Harvest API进行身份验证时出现错误请求(400) - Bad Request (400) When Trying to Authenticate Harvest API in React 尝试使用 React 从 SpringBoot 获取时出现 400 错误请求 - 400 Bad request when trying to fetch from SpringBoot using React 在 React 和 Flask 之间上传文件时 POST 400(错误请求) - POST 400 (BAD REQUEST) when uploading file between React and Flask redux 请求失败,状态码为 400(错误请求) - redux request failed with status code 400(bad request) 来自下一个/图像的 400 错误请求 - 400 bad request from next/image 快速单击时 POST 400 错误请求 - POST 400 bad request when clicking fast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM