How to set base url for swagger with flask_restplus? - python

I'm serving a flask app with flask_restplus from an apache server. I'm using ProxyPass to forward all traffic to the app at some url extension like so in the apache .conf file:
ProxyPass /some-extension http://127.0.0.1:3000
ProxyPassReverse /some-extension http://127.0.0.1:3000
The flask_restplus api is set up as follows:
from flask_restplus import Api
api = Api(
title='App Title',
doc='/docs'
)
The app works ok except that when I go to the swagger route /some-extension/docs the flask server starts looking for swagger dependencies at the url base /swaggerui/ (rather than the required /some-extension/swaggerui), and so the swagger UI fails to load.
Is there a way to configure flask_restplus (or otherwise) so that swagger gets served /some-extension/swaggerui/ (rather than the root url)?

OK, got there after some fiddling. You need to proxy traffic on to the extension and set up a flask with a blueprint so the whole app also runs from that extension. So something like this in your apache config:
ProxyPass /some-extension http://127.0.0.1:3000/some-extension
ProxyPassReverse /some-extension http://127.0.0.1:3000/some-extension
...and you have to ProxyPass the extension /swaggerui like so in your apache configuration:
ProxyPass /swaggerui http://127.0.0.1:3000/swaggerui
ProxyPassReverse /swaggerui http://127.0.0.1:3000/swaggerui
...and then use this pattern in your flask_restplus set up (from official guide):
from flask import Flask, Blueprint
from flask_restplus import Api
app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/some-extension')
api = Api(blueprint, doc='/docs/')
app.register_blueprint(blueprint)

Related

How can i use two waitress server for django app

I want to have 2 waitress servers for hosting django web application.One for serving statics and media files of django app and other for django on different ports.
I tried some thing like this and hit localhost:6003 in browser,but it is serving only static web page at 6003 and is not interacting with app running at 9090. what am i missing?
I have successfully deployed this app usig nginx-waitress architecture.But I want to deploy using waitress -waitress architecture.
app.py
from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__,)
CORS(app)
main.py
from flask import Blueprint, send_from_directory
from waitress import serve
from app import app
#app.route('/stat/<path:filename>')
def static_file(filename):
return send_from_directory("D:/static/",filename)
#app.route('/med/<path:filename>')
def media_file(filename):
return send_from_directory("D:/media/",filename)
serve(app, host='localhost', port=6003)
runserver.py
from waitress import serve
from sarus_project.wsgi import application
if __name__ == '__main__':
serve(application, host = 'localhost', port='9090')

How to allow Cross Origin Request with Flask CORS correctly?

I'm trying to allow Same Origin request to my Flask app:
this is in my __init__.py file:
# ... other imports ...
from flask_cors import CORS
cors = CORS()
def create_app(script_info=None):
app = Flask(__name__)
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)
from project.api.flakes import flakes_blueprint
app.register_blueprint(flakes_blueprint)
cors.init_app(flakes_blueprint, resources={r"/flakes": {"origins": "*"}})
return app
According to docs this should be sufficient to get it working but I get:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5001/flakes. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
How can I get it working ? I would've thought {"origins": "*"} covers everything.
I've not seen an example like you have provided using cors_init_app(), so cannot comment on why it doesn't work.
However, here is an example of I've used previously that works:
app = Flask(__name__)
CORS(app, resources={r'/*': {'origins': '*'}})
app.config['CORS_HEADERS'] = 'Content-Type'
app.register_blueprint(my_blueprint_module.bp)

Flask serving static folder with CORS

In my frontend app I'm trying to access a file I'm serving in a static folder but I'm getting an issue with CORS. The issue I'm seeing is
Access to XMLHttpRequest at 'http://127.0.0.1:5000/static_folder/apple.xml' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My backend Flask app has the following set
app = Flask(__name__, static_url_path='/static_folder', static_folder="static_folder")
It seems that I need to add the 'Access-Control-Allow-Origin' header to my responses when returning the files. Does Flask have a global way to set this?
I've tried using flask_cors but haven't been successful with
from flask_cors import CORS, cross_origin
app = Flask(__name__, static_url_path='/static_folder', static_folder="static_folder")
CORS(app)
I was able to get something similar working with the SimpleHTTPRequestHandler by setting the CORS headers as
def end_headers (self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
But I'd like to use flask to accomplish this rather than starting a simple http server.
after cors(app) add these lines:
#app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, OPTIONS')
return response
Also, if using the flask-cors package, you should be able to blanket your static requests:
from flask import Flask
from flask_cors import CORS
# Initialize
app = Flask(__name__)
cors = CORS(app, resources={r"/static/*": {"origins": "*"}})
Good luck!

Problem with CORS, not allowing communication

CORS not allowing communication. Headers not found
Using Python 3.7 and the webapp is hosted in pythonanywhere.com
This is how I'm calling CORS in my code:
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
#app.route('/', methods=['GET', 'POST'])
....
....
The Error I'm getting is this:
"Access to XMLHttpRequest at 'https‍://user.pythonanywhere.com/' from origin 'http‍://www.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
Please assist. Don't know what I am missing or where. It says header is missing. How do I add it to allow communication? Where do I add it?
Let the CORS for all the resources in the app.
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
Also, please make sure you don't have any other CORS browser extension that is doing the hampering the module you have in the program.

flask-cors not sending preflight CORS request

I want to allow only localhost as access-control-allow-origin in my Flask app. I tried searching for this issue at other places with no solution. My code is very simple and straightforward as follows :
from flask import Flask
from routes.routes import *
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "http://localhost:3000"}})
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/drax')
app.add_url_rule('/template/<actionId>/<actionName>', None, templateActions, methods=['GET'])
The above should ONLY allow any request from localhost:3000 and not from anywhere else, like localhost:8080. But even when I make the request from port 8080 (another web app), it allows the request.
Looks like you need to specify a valid http origin in your request as under:
CORS(
app,
supports_credentials=True,
resources={
r"/*": {
"origins": [
"http://localhost:3000",
""http://localhost"
"http://127.0.0.1:3000",
"http://127.0.0.1,
]
}
},
)
I do not think http://local is a valid origin

Categories

Resources