简体   繁体   中英

flassger Auto validation (validation=True)

I would like to ask help from the community regarding using flasgger on python (v3) Currently I'm using flasgger with Flask-Restplus (though flask-resplus do have it's own Swagger, I prefer to use flasgger). Anyway, my problem is when turning on the "validation=True" in @swag_from. Here are my code snippets.

Python code:

@api.route("/v1/cancels")
class Cancels(Resource):
    @swag_from(v1swag["cancels_post"], validation=True)
    def post(self):
        token = request.form.get("token")
        <...>
        message = {
            "message": "ok",
            "token": token
        }
        return jsonify(message)

Swagger (json)

v1swag = {
    "cancels_post": {
        "tags": ["/api/v1"],
        "parameters": [
            {
                "name": "token",
                "in": "body",
                "required": True,
                "description": "Cancels the provided token."
            }
        ],
        "responses": {
            "200": {
                "description": "Success!",
            }
        }
    }
}

The problem is that if if only use @swag_from(v1swag["cancels_post"]), everything works fine (except for the validation). I mean the Post transaction finishes successfully.

If I set it to @swag_from(v1swag["cancels_post"], validation=True) I get...

Response Body
{
  "message": "The browser (or proxy) sent a request that this server could not understand."
}
Response Code
400

I've also tried

"in": "formData"

But got these

Response Body
No data to validate
Response Code
500
Response Headers
{
  "access-control-allow-origin": "http://192.168.1.236:4000",
  "date": "Fri, 14 Jul 2017 08:58:19 GMT",
  "server": "gunicorn/19.7.1",
  "connection": "keep-alive",
  "content-length": "19",
  "vary": "Origin",
  "content-type": "text/html; charset=utf-8"
}

Could you please shed some light on this issue I'm facing. I still can't find a link that could help me fix the issue.

Thank you in advance everyone.

Sorry, it turns out I was missing the "schema" tag to identify and enumerate the "fields" or "tags" to validate.

Got another question regarding how to create a validation for formData but will be asking that on another post.

This post can now be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM