无法将文件发送到Imgur [英] Trouble sending a file to Imgur

查看:78
本文介绍了无法将文件发送到Imgur的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 imgur api 将图像上传到Imgur. api返回400,表示该文件不是受支持的文件类型或已损坏.我不认为该图像已损坏(我可以在本地查看它很好),并且我尝试了.jpg.jpeg.png.这是代码:

I'm trying to use the python requests lib to upload an image to Imgur using the imgur api. The api returns a 400, saying that the file is either not a supported file type or is corrupt. I don't think the image is corrupt (I can view it fine locally), and I've tried .jpg, .jpeg, and .png. Here is the code:

api_key = "4adaaf1bd8caec42a5b007405e829eb0"
url = "http://api.imgur.com/2/upload.json"
r = requests.post(url, data={'key': api_key, 'image':{'file': ('test.png', open('test.png', 'rb'))}})

确切的错误消息:

{"error":{"message":"Image format not supported, or image is corrupt.","request":"\/2\/upload.json","method":"post","format":"json","parameters":"image = file, key = 4adaaf1bd8caec42a5b007405e829eb0"}}

让我知道是否可以提供更多信息.我对Python非常了解,并且希望这是一个简单的错误操作,请有人解释一下吗?

Let me know if I can provide more information. I'm pretty green with Python, and expect it's some simple misstep, could someone please explain what?

推荐答案

我只是在猜测,但是看着imgur api,看起来图像应该只是文件数据,而请求库则将其包装到一个键值对(因此响应为何显示"image = file")

I'm just guessing, but looking at the imgur api, it looks like image should be just the file data, while the requests library is wrapping it into a key value pair (hence why the response shows "image = file")

我会尝试:

import base64
api_key = "4adaaf1bd8caec42a5b007405e829eb0"
url = "http://api.imgur.com/2/upload.json"
fh = open('test.png', 'rb');
base64img = base64.b64encode(fh.read())
r = requests.post(url, data={'key': api_key, 'image':base64img})

这篇关于无法将文件发送到Imgur的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆