猿问

Python Swagger 响应变为空(Flask、Flask_Restplus)

我可以在终端中查看响应内容,但在招摇的 UI 中,它显示为空。

问题:为了调试,我尝试从ret函数中的变量打印值json_response_from_dict(dict_),它向我显示了我通过 swagger UI 输入的值。但是在 Swagger UI 的响应中,它是空的。

但是在终端中的响应中,它显示<Response 141 bytes [200 OK]>并且终端中 ret 的值也显示{"rec_id": 464, "frame_id_prob": [[1, 0.1], [2, 0.1], [3, 0.1], [4, 0.1], [5, 0.6]], "comment": "these frames suit everyone", "mine_id": NaN}我无法弄清楚该值在哪里丢失。


MCVE:


from flask import Flask, Blueprint, request, Response

from flask_restplus import Resource, Api, fields

from flask.views import MethodView

import json

import numpy as np

def json_response_from_dict(dict_):

    """converts a python dictionary to a json string, and returns

    a HHTP response for a JSON string

    dict_ -- input python dictionary

    """


    ret = json.dumps(dict_)

    print(ret)

    resp = Response(response=ret,

                    status=200,

                    mimetype="application/json")

    print(resp)

    return resp



app = Flask(__name__)




api_v1 = Blueprint('api', __name__, url_prefix='/api/1')


api = Api(api_v1, version='1.0', title='my API',

    description='my API',

)

ns = api.namespace('my', description='my operations')


myModel = api.model('my Model', {

    'rec_id': fields.Integer(readOnly=True, description='Random Choice'),

    'comment': fields.String(required=True, description='Comments'),

    'mine_id' : fields.String(required=True, description='unique ECP ID')

})



# Register blueprint at URL

# (URL must match the one given to factory function above)

app.register_blueprint(api_v1)

但同样的问题仍然存在。


狐的传说
浏览 328回答 1
1回答

白衣染霜花

@ns.route("/dev/get_rec_id", methods=["POST"])@ns.param('mine_id', 'unique ECP ID')class RecommendationService(Resource):&nbsp; &nbsp; @ns.doc('path to generate a unique recommendationid, and to determine which frames predictions can be made for. Expected/optional input :&nbsp; &nbsp; &nbsp; &nbsp;JSON string as a https html data objects with keys: mine_id -- unique ECP ID. If this is not provided generic frames recommendations will be provided.')&nbsp; &nbsp; @ns.marshal_list_with(myModel)&nbsp; &nbsp; def post(self):&nbsp; &nbsp; &nbsp; &nbsp; mine_id = np.nan&nbsp; &nbsp; &nbsp; &nbsp; if request.is_json:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine_id = request.json.get('mine_id', np.nan)&nbsp; &nbsp; &nbsp; &nbsp; return {'rec_id': np.random.choice(1000),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fun_id_prob': [[1, 0.1], [2, 0.1], [3, 0.1], [4, 0.1], [5, 0.6]],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'comment': 'these games suit everyone',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mine_id': mine_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}在此签名不匹配即fun_id_prob不在 API 签名中,也由于某种原因在调用X-Fields时,它需要保持为空。只需使用@ns.route("/dev/get_rec_id", methods=["POST"])@ns.param('mine_id', 'unique ECP ID')class RecommendationService(Resource):&nbsp; &nbsp; @ns.doc('path to generate a unique recommendationid, and to determine which frames predictions can be made for. Expected/optional input :&nbsp; &nbsp; &nbsp; &nbsp;JSON string as a https html data objects with keys: mine_id -- unique ECP ID. If this is not provided generic frames recommendations will be provided.')&nbsp; &nbsp; @ns.marshal_list_with(myModel)&nbsp; &nbsp; def post(self):&nbsp; &nbsp; &nbsp; &nbsp; mine_id = np.nan&nbsp; &nbsp; &nbsp; &nbsp; if request.is_json:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine_id = request.json.get('mine_id', np.nan)&nbsp; &nbsp; &nbsp; &nbsp; return {'rec_id': np.random.choice(1000),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'comment': 'these games suit everyone',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mine_id': mine_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}或将列表添加到签名中,它将起作用:)。
随时随地看视频慕课网APP

相关分类

Python
我要回答