Authorization:Bearer<TOKEN>

Hi I am a engineer in the automotive industry so have experience of coding machines and robots but I am not a developer so to speak. I am trying to learn more about SAFE but my JS is limited.

I have started working through the maid safe documentation.
https://api.safedev.org/auth/validate-app-authorization.html

I have managed to play around with the post auth script and run a .js file to make a request to my SAFE Launcher. Very cool.

I am having a little trouble with Authorization:Bearer.

The documentation says it must be present in the request but I am not sure what this means.

I have tried including it in the /get script along with the token generated from the safe launcher auth but to no avail.

Can anyone offer a little more insight?

Cheers

1 Like

Hello @guybrows,

and welcome to the Dev Forum. Glad to see you are playing around with the API.

The Authorization is a HTTP Header field, meaning it is part of the Requests “metadata” (so to speak) in a key-value format. In this case the key is Authorization and the value is "Bearer " + TOKEN. You need to explicitly set this field whenever you do a request. Depending on the language and library you are using this might be through a separate function or a call parameter.

If you are using the new Javascript fetch API you can manage them through the Headers object:

var myHeaders = new Headers();
myHeaders.set('Authorization', "Bearer " + token);

var myInit = { method: 'GET',
               headers: myHeaders,
               mode: 'cors',
               cache: 'default' };

fetch('flowers.jpg',myInit)
.then(function(response) {
  return response.blob();
})
.then(function(myBlob) {
  var objectURL = URL.createObjectURL(myBlob);
  myImage.src = objectURL;
});

BTW, you can find a great example in the Javascript safe-js API library.

1 Like

Great stuff, thanks for the swift reply. I shall have a play with this tonight.

1 Like