简体   繁体   English

无法通过 POST 请求发送数据

[英]Trouble Sending data via a POST request

I have to send some data at an endpoint (flask endpoint).我必须在端点(烧瓶端点)发送一些数据。 The data I need to send is an image and a variable.我需要发送的数据是一个图像和一个变量。 I am using the below code to send the data but I am getting an error AttributeError: 'int' object has no attribute 'read'我正在使用以下代码发送数据,但出现错误AttributeError: 'int' object has no attribute 'read'

Sender Code发件人代码

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

Reciever End Point接收器端点

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

I tried the following changes but that results in other error TypeError: Object of type BufferedReader is not JSON serializable我尝试了以下更改,但导致其他错误TypeError: Object of type BufferedReader is not JSON serializable

Changes #1变化 #1

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

Changes #2变化#2

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

Changes #3变化#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)

I can easily send and receive image but I am unable to send variable data我可以轻松发送和接收图像,但我无法发送可变数据

I can send data (image and variable) easily with Postman App我可以使用Postman App轻松发送数据(图像和变量)

在此处输入图片说明

Fixed issue by using below code使用以下代码修复了问题

Sender Code发件人代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM