最近,我一直在尝试使用flask-restx添加api。我遇到了一些麻烦,甚至让helloworld也能工作。
我把所有东西都放在蓝图中,所以只有把api放在它自己的蓝图中才有意义。下面是我的api_routes.py
from flask_login import current_user, login_user, logout_user, login_required
from flask_restx import Resource, Api
from flask import Blueprint
import flask
from . import db
api_bp = Blueprint('api_bp', __name__, url_prefix='/api')
api = Api(api_bp, version='1.0', title='Scribe API',
description='An API for interfacing with Scribe',
)
@api_bp.route("/hello")
class HelloWorld(Resource):
def get(self):
return "Hello"
api.add_resource(HelloWorld, '/hello')但是,如果您转到swagger ui并发送get,如下所示:
curl -X GET "http://127.0.0.1:5000/api/hello" -H "accept: application/json"响应是500:
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a HelloWorld.因此,出于某种奇怪的原因,我返回的是类,而不是实际返回的字符串"Hello“。有没有人知道这是为什么,以及如何解决这个问题?
发布于 2020-03-09 04:17:35
在我发布这篇文章后不久,我决定继续下去,原来命名空间是强制的,你可以用下面的代码修复我上面的代码来创建一个命名空间。这将使基本的helloworld正常工作。
hello_ns = api.namespace('', description='Hello World Operations')
@hello_ns.route("/hello")
class HelloWorld(Resource):
def get(self):
return "Hello"https://stackoverflow.com/questions/60591646
复制相似问题