Skip to content

Instantly share code, notes, and snippets.

@danigm
Created January 31, 2018 08:49
Show Gist options
  • Save danigm/f7e50543d99278dad7255f299fe2cae6 to your computer and use it in GitHub Desktop.
Save danigm/f7e50543d99278dad7255f299fe2cae6 to your computer and use it in GitHub Desktop.
hug cors example
import hug
from hug.middleware import CORSMiddleware
api = hug.API(__name__)
api.http.add_middleware(CORSMiddleware(api))
@hug.post('/demo')
def demo(name: 'your name'):
return {"result": "Hello {0}".format(name)}
<html>
<body>
<script>
let url = 'http://localhost:8000/demo';
let data = {'name': 'danigm'};
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json'
})
})
.then(resp => resp.json())
.then(js => console.log(js));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment