python - 客户端通过 send_file 命令调用服务器的 FLASK 端点时无法查看图像
问题描述
我有一个在 python 中运行的 Flask 应用程序。有一个客户端正在从磁盘提供图像。我希望服务器到达终点并检索图像。
在客户端:
@app.route('/api/get_image')
def get(self, id):
# Map id to filename.
return send_file(filename)
当我从浏览器调用客户端的端点时,这有效。
在服务器端,我有一个类似的函数应该调用上述端点。
@app.route('/api/get_image')
def get(self, id):
return requests.get(client_url + '/api/get_image')
当我调用服务器端点时,我得到:
TypeError: <Response [200]> is not JSON serializable
但即使我尝试 .json() 也不起作用。如何返回图像有效负载?
解决方案
Don't return the object from requests.get()
directly, instead retrieve the .raw
or .content
object from it
r = requests.get(url)
r.raise_for_status() # raise an Exception if the request failed
return r.content
推荐阅读
- java - 如何使用 Selenium 从 xpath 中删除特殊字符?
- haskell - tup :: [String] -> [(Int, Int)]
- php - Laravel 多对多 - 不同的数据库
- c# - 通过 API 发送/上传文件 - .Net Core
- c# - ASP MVC Core 3x 异步数据库调用 - 我没有得到什么?
- python - 如何知道合规的 google-cloud-* 包?
- amazon-web-services - “限制”是否会减少 AWS Athena 上的扫描数据量?
- python - 关于 ruamel.* pypi 包的标签“ruamel”来自哪里?
- typescript - Typescript typeguard 意外行为
- python - extracting characters from python string using variable index