我尝试通过REST API发布图像以对其进行处理。我使用falcon作为后端,但不知道如何发布和接收数据。
这就是我目前发送文件的方式
img = open('img.png', 'rb')
r = requests.post("http://localhost:8000/rec",
files={'file':img},
data = {'apikey' : 'bla'})然而,在Falcon repo上,他们说Falcon不支持HTML forms来发送数据,相反,它的目标是所有的POSTed和PUTed数据,我没有区分POSTed图像数据和上面发送的数据。
因此,最终,我想了解发送图像和通过REST API接收图像的正确解决方法是什么,该API应该是由Falcon编写的。你能给我一些建议吗?
发布于 2017-01-16 20:26:15
为此,您可以使用以下方法:
Falcon API代码:
import falcon
import base64
import json
app = falcon.API()
app.add_route("/rec/", GetImage())
class GetImage:
def on_post(self, req, res):
json_data = json.loads(req.stream.read().decode('utf8'))
image_url = json_data['image_name']
base64encoded_image = json_data['image_data']
with open(image_url, "wb") as fh:
fh.write(base64.b64decode(base64encoded_image))
res.status = falcon.HTTP_203
res.body = json.dumps({'status': 1, 'message': 'success'})对于API调用:
import requests
import base64
with open("yourfile.png", "rb") as image_file:
encoded_image = base64.b64encode(image_file.read())
r = requests.post("http://localhost:8000/rec/",
data={'image_name':'yourfile.png',
'image_data':encoded_image
}
)
print(r.status_code, r.reason)我希望这能有所帮助。
https://stackoverflow.com/questions/38848575
复制相似问题