Which ajax should I use for POST && I need to add a Bearer token

Which ajax should I use for POST && I need to add a Bearer token
0.0 0


The app I am working on, interfaces with a server that uses POST with authentication. type Bearer

I not sure how to get started, I can use basic API request (for example “Quote of the day” app)

Does anyone have any example code using this type of authentication.?

thanx Glenn

xhttp.open("POST", "http://thewebsite", true);
xhttp.setRequestHeader("authentication", "Bearer " + token);

<some code to add ("authentication", "Bearer " + token);
$.post("http://thewebsite", "",function(data, status){


I don’t know the exact answer off the top of my head - but one trick I use when looking for a model code sample if I already know a fragment of what I’m trying to do is to search GitHub.

Maybe these will help: https://github.com/search?q=xhttp.setRequestHeader("authentication"%2C+"Bearer+&type=Code&utf8=✓


You can use pretty much any method/library or wrapper you like.

As you said all you have to do, beside passing the relevant data, is to add an authentication header to the request.

// ajax

    url: 'YourRestEndPoint',
    headers: {
        'Authorization': `Bearer ${token}`,
    method: 'POST',
    data: YourData,
    success: function(data){
      console.log('succes: '+data);

// axios
  method: 'post',
  url: yourEndpoint,
  data: yourData,
  headers: { 'authorization': `Bearer ${token}` }

// and any other you prefer | like


Cool, thanks for the help.

Also, I will use git for help in the future too.

  1. Is there a reason for the the back ticks around the bearer + ${token}?

  2. do I use a string assignment for the token = “tokenString…abcdef”;



The backticks are a (kind of) new syntax called template literals; pretty much is a different way to write strings with expressions and interpolation.

It’s in my opinion a cleaner and easier way to write strings; something like:

var a = 5;
var b = 10;

// usual way to write strings with variables
var s = a + ' plus ' + b + ' is equal to ' + (a + b);

// with template literals
var t = `${a} plus ${b} is euqal to ${a + b}`;

So those are equivalent.

'Authorization': `Bearer ${token}`,

'Authorization': 'Bearer ' + token,

hope it clarify things a bit.