简体   繁体   中英

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. I will write function which add necessary data to json file later, so now I need returning of an empty json file after request

This would be the easiest way to respond with 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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