无法通过POST请求发送数据

时间:2020-11-09 06:59:44

标签: python python-3.x flask postman

我必须在端点(烧瓶端点)上发送一些数据。我需要发送的数据是图像和变量。我正在使用以下代码发送数据,但出现错误AttributeError: 'int' object has no attribute 'read'

发件人代码

data = {'image': open(fl, 'rb'), 'var': 0}
res = requests.post(url, files=data)

收件人终点

file = request.files['image']
f1 = file.read()
f2 = np.fromstring(f1, np.uint8)
f3 = cv.imdecode(f2, cv.IMREAD_COLOR)

var = int(request.form.get('var'))

我尝试了以下更改,但导致其他错误TypeError: Object of type BufferedReader is not JSON serializable

更改#1

data = {'image': open(fl, 'rb'), 'var': 0}
res = requests.post(url, json=data)

更改#2

data = {'image': open(fl, 'rb'), 'var': 0}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
res = requests.post(url, json=data, headers=headers)

更改#3

data = {'image': open(fl, 'rb'), 'var': 0}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
res = requests.post(url, data=json.dumps(data), headers=headers)

我可以轻松发送和接收图像,但是无法发送可变数据

我可以使用Postman App轻松发送数据(图像和变量)

enter image description here

1 个答案:

答案 0 :(得分:0)

使用以下代码解决问题

发件人代码

    "code": 4,
    "message": "QueryUsers failed with error: \"QueryUsers failed with error: \"QueryUsers failed with error: \"QueryUsers failed with error: \"QueryUsers failed with error: \"QueryUsers failed with error: \"QueryUsers failed with error: \"Missing request payload\"\"\"\"\"\"\"",
    "StatusCode": 400,
    "duration": "0.00ms",
    "more_info": "https://getstream.io/chat/docs/api_errors_response"
}