You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tl;dr if you do not care on the below topics, you may use request.ctx in the middleware, and add json there, and use it in all your handlers.
@app.middleware('request')
async def your_middleware(request):
if request.json is None:
request.ctx.json = {}
else:
request.ctx.json = request.json
BUT
I think your HTTP request is improper as there is no data in body (empty string is not a valid JSON, according to my knowledge).
But then, why Sanic ever accepted such invalid request?
You should send valid JSON if content is set to application/json:
curl -X POST -H "Content-Type: application/json" --data '{}' http://localhost:8080/user/list
#or
curl -X POST -H "Content-Type: application/json" --data 'null' http://localhost:8080/user/list
However if you send null, it would generate the same issue (request.json == None).
Anyway, above shows that None != {} (that is, in JSON terms: null != {}), so I think it makes sense to have this distinction.
Is there an existing issue for this?
Is your feature request related to a problem? Please describe.
I have an API about this, similar to fuzzy query,
Expect all results returned:
curl -X POST -H "Content-Type: application/json" http://localhost:8080/user/list
Expect results related to test to be returned:
curl -X POST -H "Content-Type: application/json" -d '{ "name": "test"}' http://localhost:8080/user
However, the reality is that when using the first curl, the system reports an error message:
Describe the solution you'd like
When trying to process requst.json in middleware, it prompts: AttributeError: property 'json' of 'Request' object has no setter。
Is there any other good solution that does not require judging request.json to be None in app.route?
Additional context
No response
The text was updated successfully, but these errors were encountered: