how i can pass image as argument in python request along with url as 2nd argument?

余生颓废 提交于 2020-07-08 00:47:27

问题


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

here is my code:

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)

//i also tried this

img=image

and then passed:

 outPut_Data=requests.post(url,img)

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

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?


回答1:


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)

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']


来源:https://stackoverflow.com/questions/62756044/how-i-can-pass-image-as-argument-in-python-request-along-with-url-as-2nd-argumen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!