Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass X-Auth-Token in the HTTP header in JavaScript [closed]

I have a website which uses Token-based authentication. At first, username and password are sent to log the user in and receive the token. For subsequent calls, I need to include the token as X-Auth-Token in HTTP header.

I would like to know how to do that, in vanilla JavaScript or using jQuery. Could you please provide me a sample of code?

like image 278
GibboK Avatar asked Nov 12 '13 11:11

GibboK


People also ask

How do I submit auth token in header?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

How do I pass the Authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

Is it safe to send auth token in header?

In JWT token authentication, the server-provided token should always be sent as a header with the Authorization: Bearer <token> format. The website then should check the validity of the token when a request comes and handle it accordingly. Yes, the use of HTTPS is mandatory.

Why do we pass token in header?

This is common practice with authorization methods like OAuth. The API can authorize the request before accepting the contents. This can save some processing overhead by not accepting request that have not been validated by their headers.


1 Answers

In jQuery it would be something like this:

$.ajax({
   url : myurl,
   headers: {
        'X-Auth-Token' : token
   });

More details on what you can do with $.ajax() are in the docs

like image 115
asiviero Avatar answered Sep 19 '22 18:09

asiviero