-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1449 from chenjr0719/add_amending_request_object_…
…example Add example of amending request object
- Loading branch information
Showing
4 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from sanic import Sanic | ||
from sanic.response import text | ||
from random import randint | ||
|
||
app = Sanic() | ||
|
||
|
||
@app.middleware('request') | ||
def append_request(request): | ||
# Add new key with random value | ||
request['num'] = randint(0, 100) | ||
|
||
|
||
@app.get('/pop') | ||
def pop_handler(request): | ||
# Pop key from request object | ||
num = request.pop('num') | ||
return text(num) | ||
|
||
|
||
@app.get('/key_exist') | ||
def key_exist_handler(request): | ||
# Check the key is exist or not | ||
if 'num' in request: | ||
return text('num exist in request') | ||
|
||
return text('num does not exist in reqeust') | ||
|
||
|
||
app.run(host="0.0.0.0", port=8000, debug=True) |