sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

使用注入的方式封装获取的json数据,结果是空的

f754699 opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

我封装了一个类,用来获取json数据,然后将这个类注入到app中。结果在获取的json是None。

I encapsulated a class to retrieve JSON data and then injected this class into the app. The result is that the obtained JSON is None.

Code snippet

# class
class JSonData:
    data = None

    def __init__(self, data: Optional[dict] = None):
        self.data = data

    @classmethod
    def get_json_data(cls, request: Request):
        try:
            data = request.json or {}
        except:
            data = {}
        return cls(data)

# -----------
# 获取请求体json数据
app.ext.add_dependency(JSonData, JSonData.get_json_data)

Expected Behavior

request.json=None

How do you run Sanic?

Sanic CLI

Operating System

Windows

Sanic Version

23.6.0

Additional context

我在视图函数中打印request.json,结果就是有数据的,
应该是时间问题

I print request.json in the view function, and the result is that there is data,

It should be a matter of time

我知道了,需要等待。