简体   繁体   English

flask-restful API 中的多个可选参数?

[英]Multiple optional parameters in flask-restful API?

I have a very simple API. You give it a name, email, address, age, and it returns:我有一个非常简单的 API。你给它一个名字,email,地址,年龄,它返回:


{
    'your name is':name,
    'your email is': email,
    'your address is': address,
    'your age is': age
}

However I am trying to make these parameters optional.但是我试图让这些参数可选。

So I can call the API with http://127.0.0.1:5000/overflow_test/name=John ie name only.因此,我可以使用http://127.0.0.1:5000/overflow_test/name=John来调用 API,即名称。 Or I can call with just name and email http://127.0.0.1:5000/overflow_test/name=John&email='email@gmail.com或者我可以只用名字和 email http://127.0.0.1:5000/overflow_test/name=John&email='email@gmail.com

I believe my problem lies in the api.add_resource section below:我相信我的问题在于下面的api.add_resource部分:

from flask import Flask
from flask_restful import Resource, Api
import requests

app = Flask(__name__)

api = Api(app)

class hlr_lookup(Resource):

    def get(self,name,email,address,age):

        return {
            'your name is':name,
            'your email is': email,
            'your address is': address,
            'your age is': age
        }

api.add_resource(hlr_lookup,'/overflow_test/<string:name><string:email><string:address><string:age>')


if __name__ == '__main__':
    app.run(debug=True)

I have tried many different variations of that add_resource line.我已经尝试了该add_resource行的许多不同变体。

I have checked related questions here, but the answers all structure the request like this: '/overflow_test/parameter1/parameter2/parameter3/paramter4' ie all the parameters are separated by a slash / .我在这里检查了相关问题,但答案都是这样构造请求的: '/overflow_test/parameter1/parameter2/parameter3/paramter4'即所有参数都用斜杠/分隔。

Is it possible to do it separated by ampersands?是否可以用&符号分隔? & ? & It's the style of API I'm more used to using.是我比较习惯用的API的样式。

http://127.0.0.1:5000/overflow_test/name=John&email='email@gmail.com

The current output if I just use the name John :当前 output 如果我只使用名字John

# api call

http://127.0.0.1:5000/overflow_test/name=John


# result

{
    "your name is": "name=J",
    "your email is": "o",
    "your address is": "h",
    "your age is": "n"
}

Solved by using flask-resful's reqparse.通过使用 flask-resful 的reqparse.

More info here: https://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful更多信息在这里: https://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful

from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse


app = Flask(__name__)

api = Api(app)

class print_stuff(Resource):


    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, default='')
        self.reqparse.add_argument('email', type = str, default='')
        self.reqparse.add_argument('address', type = str, default='')
        self.reqparse.add_argument('age', type = str, default='')
        super(hlr_lookup, self).__init__()

    def get(self):

        args = self.reqparse.parse_args()



        return {
            'your name is':args['name'],
            'your email is': args['email'],
            'your address is': args['address'],
            'your age is': args['age']
        }

api.add_resource(print_stuff,'/overflow_test')


if __name__ == '__main__':
    app.run(debug=True)

Now I can call the api like this:现在我可以这样调用 api:

127.0.0.1:5000/print_stuff?name=John&age=23&address=main street new york&email=something@gmail.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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