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(request):
text = json.dumps(request.info)
return HttpResponse(200, [(b'content-type', b'application/json')], text_writer(text))
async def set_info(request):
text = await text_reader(request.body)
data = json.loads(text)
request.info.update(data)
return HttpResponse(204)
# 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-4.0.0.tar.gz
(8.2 kB
view hashes)
Built Distribution
Close
Hashes for bareasgi_cors-4.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec48af81e5c4129899b4361d1c26772ec3cab117e01e37cd4d7def96b0301bc7 |
|
MD5 | 1208ba814959afbadb9aba05bbfc737a |
|
BLAKE2b-256 | 9306cf48c488540ffd1f7c0386fd7544895798f69ccbd147b4cbed1e5c7f6fcf |