除了使用Swagger UI自动为我们的API生成交互式文档的能力之外,使用Flask-RESTplus而不是Flask-RESTful有什么真正的优势吗?
我已经在 Flask 应用程序上工作了几个星期。我今天完成了它并去部署它......现在它不会启动。
我没有添加或删除任何代码,所以假设部署过程中发生了一些变化?
无论如何,这是终端中显示的完整错误:
Traceback (most recent call last):
File "C:\Users\Kev\Documents\Projects\Docket\manage.py", line 5, in <module>
from app import create_app, db
File "C:\Users\Kev\Documents\Projects\Docket\app\__init__.py", line 21, in <module>
from app.api import api, blueprint, limiter
File "C:\Users\Kev\Documents\Projects\Docket\app\api\__init__.py", line 2, in <module>
from flask_restplus import Api
File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\lib\site-packages\flask_restplus\__init_
_.py", line 4, in <module>
from . import fields, reqparse, apidoc, inputs, cors
File "C:\Users\Kev\.virtualenvs\Docket-LasDxOWU\lib\site-packages\flask_restplus\fields.
py", line 17, in <module>
from werkzeug import cached_property
ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\Kev\.virtualen
vs\Docket-LasDxOWU\lib\site-packages\werkzeug\__init__.py)
Run Code Online (Sandbox Code Playgroud)
还有这里提到的三个文件中的代码。
manage.py …
我很好奇如何在Flask-RESTPlus中获取来自GET方法的查询参数.我没有设法在文档中找到一个例子.
我之前使用的是纯烧瓶,我这样做的方法是从烧瓶库中调用'request.args.get()'.任何想法如何在RESTPlus中实现这一目标?
如何记录预期在value字段中发布的输入正文以显示以便用户知道要发布的内容?目前使用的数据如下:
{
"customer_id": "",
"service_id": "",
"customer_name": "",
"site_name": "",
"service_type": ""
}
Run Code Online (Sandbox Code Playgroud)
我们可以用上面的 json 默认填充值吗?
代码:
post_parser = reqparse.RequestParser()
post_parser.add_argument('database', type=list, help='user data', location='json')
@ns_database.route('/insert_user')
class database(Resource):
@ns_database.expect(post_parser)
def post(self):
"""insert data"""
json_data = request.json
customer_id = json_data['customer_id']
service_id = json_data['service_id']
customer_name = json_data['customer_name']
site_name = json_data['site_name']
service_type = json_data['service_type']
Run Code Online (Sandbox Code Playgroud) 我正在记录我的响应模型,并且需要显示 api 返回字符串列表。
["user1","user2"]
Run Code Online (Sandbox Code Playgroud)
但模型需要字典(json)格式,如下:
ns.response(200,'Success', NS.model("my_get_model",[{
"name": fields.String(example="user1"),
}]))
Run Code Online (Sandbox Code Playgroud)
我试过以下代码,但没有一个工作:
ns = Namespace('My Apis')
ns.response(200,'Success', [ns.model("my_get_model",
fields.String(example="user1")
)])
Run Code Online (Sandbox Code Playgroud)
或者
ns.response(200,'Success', ["user1"])
Run Code Online (Sandbox Code Playgroud)
或者
ns.response(200,'Success', ns.model("my_get_model",fields.List(fields.String(example="user1"))))
Run Code Online (Sandbox Code Playgroud)
请指教。
我试图为我现有的 Flask 应用程序生成 swagger 文档,我Flask-RESTPlus最初尝试并发现该项目现在很丰富,并在分叉项目flask-restx https://github.com/python-restx/flask-restx 中进行了检查,但我仍然不认为他们支持 openapi 3.0
我有点困惑选择我需要的包。我希望解决一个问题,即我们不想为我们的 API 手动创建 swagger 文档,而是希望使用包自动生成。
import os
import requests
import json, yaml
from flask import Flask, after_this_request, send_file, safe_join, abort
from flask_restx import Resource, Api, fields
from flask_restx.api import Swagger
app = Flask(__name__)
api = Api(app=app, doc='/docs', version='1.0.0-oas3', title='TEST APP API',
description='TEST APP API')
response_fields = api.model('Resource', {
'value': fields.String(required=True, min_length=1, max_length=200, description='Book title')
})
@api.route('/compiler/', endpoint='compiler')
# @api.doc(params={'id': 'An ID'})
@api.doc(responses={403: 'Not Authorized'})
@api.doc(responses={402: 'Not Authorized'}) …Run Code Online (Sandbox Code Playgroud) 使用python flask_restplus有什么方法可以获得帖子并获取方法来获取并将文件(如xlsx)推送到服务器?
是否需要使用编组?
参考:https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/
这个答案给出了一般信息,但没有在python> flask> restplus上下文中:REST API文件上传
我是python的新手,我使用flask-resplus创建了一个微服务。在我的计算机和带有http的开发服务器上都能正常工作。我无法控制微服务的部署位置。在这些情况下,它似乎位于负载均衡器(不确定细节)的后面,并通过https进行了服务。
浏览器给出的实际错误:无法从服务器读取。它可能没有适当的访问控制源设置。
当我检查网络开发人员工具时,我看到它无法加载swagger.json。但是正在使用以下命令进行检查: http://hostname/api/swagger.json,而不是https。
我一直在搜索,并且遇到了有关此问题的讨论。而这似乎是可以没有我不得不更改库或配置的服务器上运行此修复程序。
但是,我仍然无法使它工作。
这就是我所拥有的:
在api文件上:
api_blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(api_blueprint, doc='/doc/', version='1.0', title='My api',
description="My api")
Run Code Online (Sandbox Code Playgroud)
在主应用程序文件上:
from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
from lib.api import api_blueprint
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.register_blueprint(api_blueprint)
Run Code Online (Sandbox Code Playgroud)
还尝试添加:
app.config['SERVER_URL'] = 'http://testfsdf.co.za' # but it dont look like is being considered
Run Code Online (Sandbox Code Playgroud)
使用flask-restplus == 0.9.2,
只要我不需要在将要部署服务的容器上进行配置(可以设置环境变量即可),即服务需要自包含,就可以理解任何解决方案。而且,如果我可以通过pip安装flask-resplus的一个版本,那么已经可以修复了。
谢谢大家
如果我尝试通过HTTPS使用Flask RestPlus提供Swagger UI,我只会在根URL上看到"No spec provided"错误消息,并且从不加载完整的Swagger UI.但是,如果我访问API端点,则会按预期返回响应.
查看错误页面的源HTML,我注意到它swagger.json是从而http://myhost/不是https://myhost/
我在restplus Github问题上发现了完全相同的问题
我已经用该页面上提到的猴子补丁暂时解决了我的问题.Swagger UI加载,并查看我看到swagger.json确实从中获取的HTML源代码https://myhost.
为什么会发生这种情况,如何在没有猴子补丁的情况下修复它?
HTTPS是由Cloudflare的"灵活"HTTPS服务提供的.
我的应用程序是Nginx背后的配置因此,并且我没有引起任何问题,据我所知:
...
http {
...
server {
location / {
charset UTF-8;
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
charset UTF-8;
proxy_intercept_errors on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:5000;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试上传一个文件,并使用 Swagger UI 以 json 格式从用户那里获取输入。我已经为此编写了以下代码。
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
type = api.model("tax", {
"tax_form": fields.String()})
@api.route('/extraction')
@api.expect(upload_parser)
class extraction(Resource):
@api.expect(type)
def post(self):
tax_form= api.payload # json input string
print(tax_form['tax_form'])
args = upload_parser.parse_args() # upload a file
uploaded_file = args['file']
output = func_extract(uploaded_file,tax_form['tax_form'])
return output, 201
Run Code Online (Sandbox Code Playgroud)
当我单独运行上述内容时,例如,如果我只上传文件或只从用户那里获取输入,则代码有效,但如果我一起执行。tax_from 返回None值,它不会将我通过 Swagger UI 输入的值作为 json 值。
flask-restplus ×10
flask ×7
python ×7
swagger ×3
openapi ×1
python-3.x ×1
rest ×1
swagger-ui ×1
upload ×1