最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add authorization token in incoming http request header - Stack Overflow

programmeradmin3浏览0评论

I'm implementing token authentication via DRF(Django-rest-framework). So far I've understood that in token authentication you exchange your credential with a token which server had had already generated for every user. Then you put that token in every request header to the API, without worrying about the cookies.

Now I know how to generate token and write to view to authenticate and issue token. However, I haven't figured out how to put token in the http header, which I suppose need to be done in front-end.

I tried to search but there doesn't seems to a clear answer on the internet how to do it.

I'm implementing token authentication via DRF(Django-rest-framework). So far I've understood that in token authentication you exchange your credential with a token which server had had already generated for every user. Then you put that token in every request header to the API, without worrying about the cookies.

Now I know how to generate token and write to view to authenticate and issue token. However, I haven't figured out how to put token in the http header, which I suppose need to be done in front-end.

I tried to search but there doesn't seems to a clear answer on the internet how to do it.

Share Improve this question edited Jul 10, 2018 at 15:29 user177800 asked Jul 10, 2018 at 12:13 GilgameshGilgamesh 1732 gold badges2 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You need to provide that token in the Authorization header

Example :

headers = {
    "Authorization": "Token " + token
}

# or, depends upon specific Token Authentication that you're using
headers = {
    "Authorization": "Bearer " + token
}

And then send this as header, something like this

response = requests.get(url, headers=headers)

for ajax requests check this out Add Header in AJAX Request with jQuery

发布评论

评论列表(0)

  1. 暂无评论