我如何在 python 请求中将图像作为参数传递,并将 url 作为第二个参数传递? [英] how i can pass image as argument in python request along with url as 2nd argument?

查看:44
本文介绍了我如何在 python 请求中将图像作为参数传递,并将 url 作为第二个参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用请求和 url 将图像从视图传递到 api.

Hi i want to pass image from view to api using request,with url.

这是我的代码:

def processImage(request):
image=request.FILES['image']
params = {'image': image }

url='http://127.0.0.1:8000/idealweight/'
outPut_Data=requests.post(url,params=params)

//我也试过这个

img=image

然后通过:

 outPut_Data=requests.post(url,img)

但没有奏效.这会在 imgFinalPro 中导致错误,传递的参数很少.

but did not worked.and this gives error in imgFinalPro few arguments passed.

我正在调用这个 imgFinalPro 函数,在这里我想接收图像:

I am calling this imgFinalPro function And here I want to receive the image:

 def imgFinalPro(request,img):
        
        print(request)
        # image=request.FILES['image']
        # image=img
        image=request.FILES['image']

我如何在这里接收图像?可以用于进一步处理吗?

How I can receive the image here? CAn use for further processing?

推荐答案

将文件发送到 files 字段而不是 params.

Send your files inside files field not inside params.

def processImage(request):
    image=request.FILES['image']
    files = {'image': image }

    url='http://127.0.0.1:8000/idealweight/'
    outPut_Data=requests.post(url, files=files)

我假设 imgFinalPro 是您发布请求的控制器.您无法将图像作为控制器的参数.图片附在您的要求中

I Assume imgFinalPro is your controller for post request. You can't get the image as params of your controller. The image is attached with your request

def imgFinalPro(request):
    print(request)
    # image=request.FILES['image']
    # image=img
    image=request.FILES['image']

这篇关于我如何在 python 请求中将图像作为参数传递,并将 url 作为第二个参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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