'flask-restplus /flask-restx automatically add 401 response to Swagger docs if authentication is on
As the title mentions, I would like to have a @api.response(401, 'Unauthenticated')
response added to the documentation of all APIs which require authentication.
flask-resplus/restx
displays a lock icon, so the user should expect a 401
if not authenticated, but I would like this response to be explicit without having to explicitly write that decorator on every method or resource.
Is this possible with some global setting?
Solution 1:[1]
+1 on that. I'm facing a somewhat similar challenge. Also opened this issue on their GitHub repo with hopes to get an answer. Would be awesome if someone could suggest a solution ??
Solution 2:[2]
you don't need to have a special decorator for it. you just need the following code in you endpoints init.py file. Swagger will automatically support authorization and will through 401 with any decorator.
from flask_restx import Api
blueprint = Blueprint("api", __name__)
authorizations = {
"Authorization": {
"description": "Inputs: Bearer \\<jwtToken\\>",
"type": "apiKey",
"in": "header",
"name": "Authorization",
}
}
api = Api(
blueprint,
title="Dummy API",
version="1.0",
authorizations=authorizations,
security="Authorization",
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Moshe Shitrit |
Solution 2 | Hashir Irfan |