image - 将图像上传到 imgur 时出错 - URL 无效
问题描述
我在使用 imgur api 时遇到了一些麻烦。我将图像转换为 base64 代码并尝试将其上传到 imgur api。不幸的是,我收到一个错误:
"error": "Invalid URL (data:image/png;base64,iVBORw0KGgoAA..."
这是我的功能:
uploadImageToImgur: function (file) {
const url = 'https://api.imgur.com/3/image',
reader = new FileReader();
reader.onloadend = async function () {
let { result } = reader;
try {
const request = await fetch(url, {
method: 'POST',
headers: {
"Authorization": 'my client key',
},
body: result
});
const response = await request.json();
console.log(response);
} catch (e) {
throw new Error(e);
}
}
if (file) {
reader.readAsDataURL(file);
}
}
解决方案
您缺少一些参数。此外,请确保您的标头具有 Client-ID 密钥。
const request = await fetch(url, {
method: 'POST',
headers: {
"Authorization": 'Client-ID {yourKey}',
},
form: {
"image": result,
"type": "base64"
}
});
推荐阅读
- spi - ADC MCP3561 不返回转换值
- xamarin.forms - r8 shrinker Java.Lang.NoSuchMethodError Message=xamarin 表单中没有非静态方法
- powershell - 如何在一行中将主机和添加内容写入日志文件?
- mysql - Golang MySQL Docker 连接被拒绝
- python - 从列表的ndarray转换为ndarray
- python - 如何将 wav 写入 tfrecord 然后读回
- javascript - 了解递归中的函数执行顺序
- node.js - 提交表单时获取空值
- ruby-on-rails - 如何获得 .save 失败的 rspec 覆盖率
- javascript - 获取特定类的第 nth-last-child()