如何在来自 django 的 post 请求中发送图像文件

问题描述

我正在构建文章写作网站,用户可以在其中撰写文章并将图片添加到他们的文章中。我正在使用 imgur.com api 及其访问令牌将图像上传到 imgur.com。

但我不能直接从 post fetching 上传图像到 imgur.com 因为它可以显示我的访问令牌,所以我想将该图像发送到我的 django 服务器(post 请求),django 可以从那里向 imgur 发出 post 请求上传已发送的图片,作为回报,我将获得图片链接。

enter image description here

我的邮递员请求。我正在将图像文件设置到 django 服务器,我将在其中将此图像发送到 imgur.com

enter image description here

django post api 代码

class ImageUploadView(APIView):
    permission_classes = [ AllowAny ]
    parser_classes = [MultiPartParser,FormParser]
    def post(self,request):
        print(request.data)
        uploaded_file = request.FILES['image']
        uploaded_file2 = str(uploaded_file.read())
        url = "https://api.imgur.com/3/upload"
        payload={}
        files=request.FILES['image']
        headers = {'Authorization': 'Bearer vghgvghvhgvhgv'}
        response = requests.request("POST",url,headers=headers,data=payload,files=files)
        print(response.text)

我收到此错误

ValueError:解包的值太多(预期为 2)

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...