Created
November 18, 2018 05:01
-
-
Save hnuzhoulin/0899c7fed0e96cf973c86089c8090f6f to your computer and use it in GitHub Desktop.
flask-restful reqparse check nested args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#! -*- encoding:utf-8 -*- | |
# Author: Zhoulin | |
# FIle: nested_RequestParser | |
# Description: This method,the nested parser do not support required. | |
# Created: 2018/11/17 23:54 | |
# Modified: | |
# 2018/11/17 23:54 : | |
# --------------------------------- | |
import traceback | |
from datetime import datetime | |
from flask import Flask, got_request_exception | |
from flask_restful import Resource, Api, reqparse | |
app = Flask(__name__) | |
api = Api(app) | |
class S3AdminAPIBase(Resource): | |
def __init__(self): | |
"""TODO: to be defined1. """ | |
self.parser = reqparse.RequestParser() | |
self.parser.add_argument('XX',required=True, | |
help='NetEase auth token required') | |
self.parser.add_argument('nested_one', type=dict) | |
self.parser.add_argument('nested_two', type=dict) | |
self.args = self.parser.parse_args() | |
class test(S3AdminAPIBase): | |
def post(self): | |
nested_one_parser = reqparse.RequestParser() | |
nested_one_parser.add_argument('id1', type=int, location=('nested_one',)) | |
nested_one_args = nested_one_parser.parse_args(req=self.args) | |
nested_two_parser = reqparse.RequestParser() | |
nested_two_parser.add_argument('id2', type=int, equired=True, location=('nested_two',)) | |
nested_two_args = nested_two_parser.parse_args(req=self.args) | |
return self.args | |
api.add_resource(test, '/test') | |
if __name__ == '__main__': | |
app.run(debug=True, host="0.0.0.0", port=5001) | |
# curl -X "POST" "http://127.0.0.1:5001/test" -H 'Content-Type: application/json; charset=utf-8' -d $'{"nested_one": {"id1":22},"XX": "zlyf"}' | |
# { | |
# "XX": "zlyf", | |
# "nested_one": { | |
# "id1": 22 | |
# }, | |
# "nested_two": null, | |
# "unparsed_arguments": {} | |
# } | |
# curl -X "POST" "http://127.0.0.1:5001/test" -H 'Content-Type: application/json; charset=utf-8' -d $'{"nested_one": {"id1":22},"XX": "zlyf","nested_two": {"id2":22}}' | |
# { | |
# "XX": "zlyf", | |
# "nested_one": { | |
# "id1": 22 | |
# }, | |
# "nested_two": { | |
# "id2": 22 | |
# }, | |
# "unparsed_arguments": {} | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment