sanic-token-auth icon indicating copy to clipboard operation
sanic-token-auth copied to clipboard

headers

Open baeriswyld opened this issue 6 years ago • 3 comments

Hello,

how do I send the key and secret in a header ?

When I do:

import requests
import json
headers =  {"X-My-App-Auth-Token": "utee3Quaaxohh1Oo"}
data = {'key':'value'}
data =json.dumps(data)
r = requests.post('http://0.0.0.0:8000/json/',data=data,headers=headers)

and on the server side:

from sanic import Sanic
from sanic import response
from sanic_token_auth import SanicTokenAuth

app = Sanic(__name__)
auth = SanicTokenAuth(app, secret_key='utee3Quaaxohh1Oo', header='X-My-App-Auth-Token')

@app.route("/json", methods=['GET', 'POST'])
@auth.auth_required
def post_json(request):
   data = request.json
   return response.json(data)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

This doesn't work. (Without header it works) Could you please let me know how to phrase the header ? Thanks

baeriswyld avatar Jul 30 '18 16:07 baeriswyld

Any update on this?

ohld avatar Feb 17 '20 21:02 ohld

Thanks for reporting the issue! I'll take a look on the weekend and get back.

saabeilin avatar Feb 18 '20 20:02 saabeilin

@baeriswyld @ohld I've just checked and confirm that it in fact does not work with synchronous handlers. However, with async def post_json(request) it works as expected. One of the main ideas behind Sanic is asyncio, so it's expected to have [most of] handlers using async.

saabeilin avatar Feb 18 '20 22:02 saabeilin