sanic icon indicating copy to clipboard operation
sanic copied to clipboard

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

Open f754699 opened this issue 6 months ago • 0 comments

Is there an existing issue for this?

  • [X] 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

f754699 avatar Jan 06 '24 08:01 f754699