使用flask传递URL中的多个参数

时间:2017-05-05 05:26:50

标签: python flask

我正在尝试在url中传递多个参数,但只有一个参数正在接受。我使用了网址curl -i http://localhost:5000/details_page/api/v1.0/recommendation?content_id=SINGTEL_movie_22937&category=details_page,但只传递了content_id

@app.route('/details_page/api/v1.0/recommendation', methods=['GET'])
def recommendation():
    category = request.args.get('category')
    content_id = request.args.get('content_id')
    print(content_id)
    print(category)
    return(content_id,category)
    if (category !='details_page'):
        abort(404)
    contents = cl.details_page(content_id)#goes to my logic
    return(jsonify({'content_id':contents}), 201)

2 个答案:

答案 0 :(得分:0)

我对你的代码进行了一些小改动,你在API中返回了两个变量,这就是为什么不打印它是否必须以字符串或JSON格式传递

字符串

app = Flask(__name__)

@app.route('/details_page/api/v1.0/recommendation', methods=['GET'])
def recommendation():
    category = request.args.get('category')
    content_id = request.args.get('content_id')
    print(content_id)
    print(category)
    return "{},{}".format(category, content_id)#changed this line
    if (category !='details_page'):
        abort(404)
    contents = cl.details_page(content_id)#goes to my logic
    return(jsonify({'content_id':contents}), 201)
#from app import app

词典

@app.route('/details_page/api/v1.0/recommendation', methods=['GET'])
def recommendation():
    category = request.args.get('category')
    content_id = request.args.get('content_id')
    print(content_id)
    print(category)
    return jsonify(
                category= category,
                content_id= content_id
           ), 200
    if (category !='details_page'):
        abort(404)
    contents = cl.details_page(content_id)#goes to my logic
    return(jsonify({'content_id':contents}), 201)
#from app import app

答案 1 :(得分:0)

我得到了答案。我的网址有问题。使用curl时,url应该在双引号内。

curl -i http://"localhost:5000/details_page/api/v1.0/recommendation?content_id=SINGTEL_movie_22937&category=details_page"