1

I have started using apidoc for generating REST api documentation. I have tried different things with it.

In my flask based api application, I use JWT (flask-jwt in particular) for authentication. The doc gets generated properly, but the request from the doc doesn't happen properly.

This is where I have put the docsting for generating the doc

def authenticate(username, password):
    """
    @api {post} /auth Authentication url
    @apiName Authentication
    @apiGroup Login

    @apiParam {String} username Username of the user
    @apiParam {String} password Password of the user

    @apiSuccess {String} access_code JWT

    @apiSuccessExample Success Response
    HTTP/1.1 200 OK
    {
      "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjM5MDA4MGExLWY0ZjctMTFlNS04NTRkLTI4ZDI0NDQyZDNlNyIsImlhdCI6MTQ1OTE3ODE0NSwibmJmIjoxNDU5MTc4MTQ1LCJleHAiOjE0NTkxNzg0NDV9.nx_1a4RmvJ7Vlf1CvnMzqoTfzChcuJnDb1Tjy1_FnXw"
    }

    @apiErrorExample Invalid Credentials
    {
      "description": "Invalid credentials",
      "error": "Bad Request",
      "status_code": 401
    }
    """
    user = session.query(User).filter_by(username=username).first()
    if user and user.is_correct_password(password):
        return user
    return False

And when I generate the doc, it get's generated properly

$ apidoc -i onehop/ -o doc
  1. But when I pass credentials from the generated doc, the request fails. The request data I get in flask's request is '' (empty)
  2. It also fails even after including the Content-Type header in the docstring. The request data I get in flask's request is username=test@example.com&password=test123

I have confirmed above by putting debug points in Flask-JWT's code

def _default_auth_request_handler():
    data = request.get_json()   # My debug poing
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)
    password = data.get(current_app.config.get('JWT_AUTH_PASSWORD_KEY'), None)

But when I make the same request using postman it happens properly.

Postman headers enter image description here

Apidoc headers enter image description here

What did I miss?

Hussain
  • 5,057
  • 6
  • 45
  • 71

0 Answers0