Giter Site home page Giter Site logo

bottle-cors-plugin's Introduction

bottle-cors-plugin

The easiest way to implement CORS on your bottle py web application.

Installing the plugin

pip install bottle-cors-plugin

Then, on your bottle app import the cors_plugin and install it like so:

# -*- coding: utf-8 -*-
from bottle import app, response, route, run
from bottle_cors_plugin import cors_plugin

@route('/', method='GET')
def landing():
  response.content_type = 'application/json'
  return {'status': 'Works'}

#Confugure the server
app = app()
app.install(cors_plugin('*'))

if name == "__main__":
  run(host='localhost', port=7000)

The cors_plugin function accepts a string or an array of strings representing the allowed origins. Passing no origins to the function will assume '*', that is, all origins are allowed.

cors_plugin()

This will allow all origins ('*').

cors_plugin('https://google.com')

Allows just "https://google.com" as origin.

cors_plugin(['https://google.com', 'http://google.com'])

Is how you define multiple allowed origins.

Aborts

For regular abort errors you need to import the abort function from cors_plugin like so:

from bottle_cors_plugin import abort


@route('/', method='GET')
def landing():
  response.content_type = 'application/json'
  abort(500, 'Hola')
  return {'status': 'Internal error'}

It works with all errors, and if you need a custom error handler, use the cors_headers function like so:

# -*- coding: utf-8 -*-
from import_env import os
from bottle import error, response
from bottle_cors_plugin import cors_headers

error_log = error

for status_code in range(200, 600):
@error(status_code)
def errorCustom(error_log):
    cors_headers()
    error_log.content_type = 'application/json'
    return error_log.body

bottle-cors-plugin's People

Contributors

alfonsocv12 avatar angelsolaorbaiceta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.