首页 > 解决方案 > python请求发送带有图像和json数据的表单数据

问题描述

    import json
    import requests
    import urllib.request as urllib2

    profile_url = 'http://127.0.0.1:8000/api/user-profile/'

    img_file1 = urllib2.urlopen('https://cgafrica-media.s3.eu-west-2.amazonaws.com/static/{}'.format(path_of_image))
    img_file2 = urllib2.urlopen('https://cgafrica-media.s3.eu-west-2.amazonaws.com/static/{}'.format(profile_banner_path))

    files=[
        (('image',img_file1.read()),'image/jpeg'),
        (('profile_banner',img_file2.read()), 'image/jpeg')
    ]

    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'token 519a585bfee6161e1f2617aa8e5be6bdeea8d5e8',
        'Cookie': 'csrftoken=OLELqHA3pBRyheGuyS3zbA3RBpfZceJq5NLPXqUrZnp7KuaHWLI3etEA1UbxOf9A'
    }


    response = requests.request("POST", profile_url, data={"username": "soub"}, files=files)

在这里,我正在尝试使用 requests 将图像和一些数据发送到上述端点。我的图像在 aws s3 上,所以我正在阅读和发送这样的图像。这是正确的方法还是我错过了什么?

它说expected string or bytes-like object

请看一下

标签: python

解决方案


推荐阅读