简体   繁体   English

我如何创建sanic服务器,在收到请求后将返回带有必要数据的json文件?

[英]How could I create sanic server which after getting request will return json file with necessary data?

I want to create sanic server which returns json file with necessary information after accepting request.我想创建 sanic 服务器,它在接受请求后返回带有必要信息的 json 文件。 I will write function which add necessary data to json file later, so now I need returning of an empty json file after request稍后我将编写将必要数据添加到 json 文件的函数,所以现在我需要在请求后返回一个空的 json 文件

This would be the easiest way to respond with json .这将是响应json的最简单方法。

from sanic import Sanic
from sanic import response

app = Sanic("my_app")

@app.get("/some-json")
async def some_json(request):
    return response.json({})

I am a little unclear about your question though.我对你的问题有点不清楚。 Are you trying to respond with json in the body of the response, or to send back a file?您是尝试在响应正文中使用json进行响应,还是发回文件?

If a file, you coulddo this如果是文件,你可以这样做

@app.get('/some-json-file')
async def some_json_file(request):
    return await response.file('/path/to/file.json')

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

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