Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
f754699 opened this issue Jan 6, 2024 · 0 comments
Open
1 task done

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

f754699 opened this issue Jan 6, 2024 · 0 comments
Labels

Comments

@f754699
Copy link

f754699 commented Jan 6, 2024

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

@f754699 f754699 added the bug label Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant