我正在创建一个简单的 Sanic REST API,但无法弄清楚如何返回数字数据。查看文档后,您似乎只能返回json
, html
,text
等,但不能返回float
or int
。
我遵循文档中的基本示例,除了将返回切换为浮点数:
from sanic import Sanic
from sanic.response import json
app = Sanic()
@app.route("/")
async def test(request):
return 1.1
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
但是,如果 return 语句是return json({"hello": "world"})
. 为什么我不能返回浮点数?我可以将其返回为return text(1.1)
,但是调用它的客户是否将其接收为 a str
(而不是 a float
)?