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

查看:518
本文介绍了我如何将图像作为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']

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

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

推荐答案

将文件发送到文件"字段而不是参数中.

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天全站免登陆