CORS support for bareasgi
Project description
bareASGI-cors
CORS support for bareASGI (read the docs)
Usage
Simply create the CORSMiddleware
class and put is as the first middleware.
import json
import uvicorn
from bareasgi import (
Application,
text_reader,
text_writer
)
from bareasgi_cors import CORSMiddleware
async def get_info(scope, info, matches, content):
text = json.dumps(info)
return 200, [(b'content-type', b'application/json')], text_writer(text)
async def set_info(scope, info, matches, content):
text = await text_reader(content)
data = json.loads(text)
info.update(data)
return 204, None, None
# Create the CORS middleware class
cors_middleware = CORSMiddleware()
# Use the CORS middleware as the first middleware.
app = Application(info={'name': 'Michael Caine'}, middlewares=[cors_middleware])
app.http_router.add({'GET'}, '/info', get_info)
app.http_router.add({'POST', 'OPTIONS'}, '/info', set_info)
uvicorn.run(app, port=9010)
The POST method
In the above example an OPTION method is included with the POST. This is always required with a POST as a browser will try first with an OPTION.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
bareasgi-cors-3.1.2.tar.gz
(8.3 kB
view hashes)
Built Distribution
Close
Hashes for bareasgi_cors-3.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 228c657d2174161c89e4ae22ad6038d0c94861e6903973ac3d93dd30dd12e991 |
|
MD5 | 6ca186f6726ecce66676c8244f2c99bd |
|
BLAKE2b-256 | 9845055ca4bfae5a6dc5e1eceaa0806de7d2906908f557a7dd32a80d2192ae87 |