Token authorization error if read from local storage ionic


I am working with Ionic, I need to send token to get the api data. The below code works fine

this.hdrsnew HttpHeaders({

      Authorization:"Token "+'d5213668d34013c9087273a5efab6d39d6d8bfb9'


but when tried to read data from local storage, not getting the api data. Showing authorization error. The code i tried is

this.hdrsnew HttpHeaders({

      Authorization:"Token "+(localStorage.getItem('token')


when console.log((localStorage.getItem(‘token’))) is tried printing the same token – “d5213668d34013c9087273a5efab6d39d6d8bfb9” Checked the type of both Both are giving string


Just Put in API rather than Local

//get code //api code (header value, where getapI CALLED

this.headers  {
      Authorization: "Token " + this.getLoginCred(),
      "Content-Type": "application/json; charsetutf-8",
      Accept: "application/json",

//my getLoginCred() function is as follows:

var data  localStorage.getItem('LoginData');
    if (data ! null && data ! '' && data ! undefined) {
      return JSON.parse(window.atob(data.split('.')[1]));
    else {
      return null;

I Do this code and my code runned Perfectly. WITHOUT ANY ERROR OR WARNING..

Answered By – Mansi Mistry

Leave a Comment