Ionic Http appending header

Hi Everyone,
I have started an app using the super star template. S far the template proved to be quite cool. Most of what was needed was already handled. The template is a great boiler plate for starting an app.
However there this part in api.ts where the initial design has already given different methods to utilize. Post get put delete etc… now in each method takes a reOpts variable which basically gives an options to send specific headers of params. I have a requirement that in some calls I need to append to header the authorization token. to keep the design clean I followed the structure that was followed initially in the template.
So I have a provider for my Trips page

import 'rxjs/add/operator/toPromise';

import { Injectable } from '@angular/core';

import { Api } from '../api/api';

import { NativeStorage } from '@ionic-native/native-storage';

@Injectable()
export class Tripserv {
  news:any;
  subSegment:any;
  subSegmentEmployee:any;
  constructor(public api: Api) { 
    this.subSegment = 'trips/';
    this.subSegmentEmployee = 'employees/trips/requests/'
  }
  listTrips(){
    let seq = this.api.get(this.subSegment+'list').share();
    return seq;
  }
  submitTrips(body){
    let reqOpts;
    reqOpts = this.api._initializeReqOpts(reqOpts);
    reqOpts = this.api._addStandardHeaders(reqOpts.headers);
    let seq = this.api.post(this.subSegmentEmployee+'save',body,reqOpts).share();
    return seq;
  }
}

Then there is the api.ts file which carries all the methods so that it can be reused in other providers.

import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
  import { RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';

/**
 * Api is a generic REST Api handler. Set your API url first.
 */
@Injectable()
export class Api {
  url: string = 'http://tobacco.perceptivemind-eg.com/tobacco-api/api/v1';
  token:any;

  constructor(public http: HttpClient) {
    this.token ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vdG9iYWNjby5wZXJjZXB0aXZlbWluZC1lZy5jb20vdG9iYWNjby1hcGkvYXBpL3YxL2FjbC91c2Vycy9sb2dpbiIsImlhdCI6MTUwOTYxNDA5MCwiZXhwIjoxNTEyMjQyMDkwLCJuYmYiOjE1MDk2MTQwOTAsImp0aSI6InE3THAxd3VFQUs2b3JuUkciLCJzdWIiOjE1LCJwcnYiOiJiMjNmNzAwYmQ1YmU1YTdjMTNjMmE4NmQ0MGZiYTUwMWZhOWVkNzczIn0.3vIYddLRiP5eyKFdmk-GWIpmY4E2PR7GlQR5s-65TXw";
    //this.header.Authorization ='Bearer '+this.token;
  }
// Custom method to add standard headers.
  public _addStandardHeaders(header:HttpHeaders)
  {
    header = header.append('Content-Type','application/json');
    header = header.append('Accept','application/json');
    header = header.append('Authorization','Bearer '+this.token);
    
    return header;
  }
// custom method to initialize reqOpts
  public _initializeReqOpts(reqOpts)
  {
    if (!reqOpts) {
      reqOpts = {
       headers: new HttpHeaders(),
       params: new HttpParams()
      };
    }
    return reqOpts;
  }
  get(endpoint: string, params?: any, reqOpts?: any) {
    return this.http.get(this.url + '/' + endpoint, reqOpts);
  }

  post(endpoint: string, body: any, reqOpts?: any) {
    return this.http.post(this.url + '/' + endpoint, body, reqOpts);
  }

  put(endpoint: string, body: any, reqOpts?: any) {
    return this.http.put(this.url + '/' + endpoint, body, reqOpts);
  }

  delete(endpoint: string, reqOpts?: any) {
    return this.http.delete(this.url + '/' + endpoint, reqOpts);
  }

  patch(endpoint: string, body: any, reqOpts?: any) {
    return this.http.put(this.url + '/' + endpoint, body, reqOpts);
  }
}

The problem is that my Authorization token is never appended to the headers. When I traced using debugger, I found that reqOpts has my appended headers in something called lazyUpdate. My http call fails as no authorization token was sent so I get an Unauthorized error.
Headers Issue

If anyone could tell me if I am doing something wrong here? How to apply the update.

I have the same problem, did you managed to resolve this?

Have you tried, if it works, when you set the headers like that?

reqOpts = {
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + this.token
  },
  params: new HttpParams()
};
2 Likes

I had a similar issue with .append(),
Using .set worked

new HttpHeaders().set('Authorization','Bearer '+this.token)

.set() should be chained to HttpHeaders()

hi I do have similar problem. I need to add custom headers but the header not working

let headers = new HttpHeaders();
//headers.append(“Content-Type”, ‘application/json; charset=utf-8’);
headers.set(‘id’, ‘105’);
headers.set(‘user_id’, ‘90’);
this.http.get(this.apiUrl, {headers})
.subscribe(
data => {
console.log(data);
},
err => {
console.log("ERROR!: ", err);
}
);

i need to pass id and user_id as custom header and the api will authenticate it. But its not working. I already updated my npm to ionic 3 and latest angular. please help.

thanks

const headers = new HttpHeaders({
  'Content-Type': 'application/json; charset=utf-8',
  id: '105',
  user_id: '90'
});

Sorry but i already do the above but it is still not working:
Here is my code:
let h = new HttpHeaders({
‘Content-Type’: ‘application/json; charset=utf-8’,
merchant_id: ‘105’,
merchant_user_id: ‘90’
});
this.http.get(this.apiUrl, {
headers: h
}).subscribe(
data => {
console.log(data);
//console.log(headers.get(‘merchant_id’));
},
err => {
console.log("ERROR!: ", err);//console.log(headers.get(‘merchant_id’));
}
);

Below is request header: still i wasnt able to send the header.

Accept:/
Accept-Encoding:gzip, deflate
Accept-Language:en-PH,en-US;q=0.9,en;q=0.8
Access-Control-Request-Headers:content-type,merchant_id,merchant_user_id
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
DNT:1
Host:name.com
Origin:http://localhost:8100
Pragma:no-cache
User-Agent:Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

IONIC INFO:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 7.0.0
Ionic Framework : ionic-angular 3.9.2

thanks again.

I don’t know headers configuration very well, but does this line say that the headers are set or what does it mean?
Access-Control-Request-Headers:content-type,merchant_id,merchant_user_id

EDIT: Okay I think that access-control-request-headers tell you that these headers are allowed by your server, right?

Are you using the HttpModule with constructor(http: Http) or the HttpClientModule with constructor(http: HttpClient)?

I am using the HttpClient and set the Authorization Header the way described. When i use ionic serve i get the following request header description in chrome dev tools:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJJbW1vU2NhbiIsInN1YiI6IjVhNmYxNTEwYTcxMmY1MDAxMjE2ODg5MyIsImlhdCI6MTUxNzQxMTIxMzgwNywiZXhwIjoxNTE3NDk3NjEzODA3fQ.N03RXGgErduEQNI2jQDNEpO9h-Mvj3Wv_6kfYae1FBg
Connection:keep-alive
Host:localhost:8100
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1

EDIT 2:
This time i tried the following:

...
static createHeadersFromToken(token: string): HttpHeaders {
    return new HttpHeaders({
      Authorization: token,
      merchant_id: '105',
      merchant_user_id: '90'
    });
  }

  constructor(public http: HttpClient) { }
...

and chrome sends this header:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJJbW1vU2NhbiIsInN1YiI6IjVhNmYxNTEwYTcxMmY1MDAxMjE2ODg5MyIsImlhdCI6MTUxNzQxMTY0MTk2MywiZXhwIjoxNTE3NDk4MDQxOTYzfQ.HfUtkp1LKY1r4EdPKxt5KWvdJYjXmvZ30xJjDcj2PUE
Connection:keep-alive
Host:localhost:8100
If-None-Match:W/"389-c6wWiRCW+lrwlad15id5Dz63lmA"
merchant_id:105
merchant_user_id:90
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1

my package dependencies as follows:
"@angular/common": “5.0.3”,
"@angular/compiler": “5.0.3”,
"@angular/compiler-cli": “5.0.3”,
"@angular/core": “5.0.3”,
"@angular/forms": “5.0.3”,
"@angular/http": “5.0.3”,

im new in http. if it is not much can i have a simple complete code wherein you httpclient?

Because i cant still send the header:
filename: app.module.ts

import { BrowserModule } from ‘@angular/platform-browser’;
import { ErrorHandler, NgModule } from ‘@angular/core’;
import { IonicApp, IonicErrorHandler, IonicModule } from ‘ionic-angular’;
import { SplashScreen } from ‘@ionic-native/splash-screen’;
import { StatusBar } from ‘@ionic-native/status-bar’;
import { HttpClientModule } from ‘@angular/common/http’;

import { MyApp } from ‘./app.component’;
import { HomePage } from ‘…/pages/home/home’;

@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

app.component.ts
import { Component } from ‘@angular/core’;
import { Platform } from ‘ionic-angular’;
import { StatusBar } from ‘@ionic-native/status-bar’;
import { SplashScreen } from ‘@ionic-native/splash-screen’;

import { HomePage } from ‘…/pages/home/home’;
import { HttpClient,HttpHeaders } from ‘@angular/common/http’;

@Component({
templateUrl: ‘app.html’
})
export class MyApp {
rootPage:any = HomePage;
apiUrl = “http://domain.com/api/v2/102”; /
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public httpClient:HttpClient) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});

    const h = new HttpHeaders().append('Authorization', 'Basic YXBpOmViYzg3Njg5MjhiZjE1NGIyMTg4NGZlMjU5MDA3NDllMGU0MTRmZGM=');
   //   this.httpClient.get(this.apiUrl, { headers });
  
      this.httpClient.get(this.apiUrl, {headers:h}).subscribe( 
        data => {
          console.log(data);
          //console.log(headers.get('merchant_id'));
        }, 
        err => {
          console.log("ERROR!: ", err);//console.log(headers.get('merchant_id')); 
        }
    );  


  }



  createHeadersFromToken(token: string): HttpHeaders {
    return new HttpHeaders({
      Authorization: token,
      merchant_id: '105',
      merchant_user_id: '90'
    });
}

}

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';

import { HomePage } from '…/pages/home/home';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = HomePage;
  apiUrl = 'http://domain.com/api/v2/102';
  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public httpClient: HttpClient) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });

    const headers = new HttpHeaders({
      Authorization: 'Basic YXBpOmViYzg3Njg5MjhiZjE1NGIyMTg4NGZlMjU5MDA3NDllMGU0MTRmZGM=',
      merchant_id: '105',
      merchant_user_id: '90'
    });

    // The response is already mapped to the body, so you have no access to the headers here.
    this.httpClient.get(this.apiUrl, { headers }).subscribe(
      response => {
        console.log('body', response);
      },
      err => {
        console.log('ERROR!: ', err);
      }
    );

    // With observe, you can request the full responseObject. It has the properties body and headers.
    // This time you can read out the headers, but these are from the response from your server, not from your request.
    // To check the headers from your request, you have to use the browser dev tools or read the headers out in your server.
    this.httpClient.get(this.apiUrl, { headers, observe: 'response' }).subscribe(
      response => {
        console.log('body', response.body);
        // This works only if your server responses with the same header.
        console.log('merchant_id', response.headers.get('merchant_id'));
      },
      (err: HttpErrorResponse) => {
        console.log('ERROR!: ', err.message);
        console.log('status', err.status);
      }
    );
  }
}

@densetsughem have you solve this issue? I have the same problem

instead of sending
Authorization:anymous
merchant_id:105
merchant_user_id:90

the request still sending Access-Control-Request-Headers & Access-Control-Request-Method
here what is like

Accept:/
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Access-Control-Request-Headers:authorization,merchant_id,merchant_user_id
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:7070
Origin:http://localhost:8100
User-Agent:Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.168 Mobile Safari/537.36

Hi Nexi,

Thanks for your example code.
I’m using your above example code to try POST method in IONIC. But getting below error.

Access to XMLHttpRequest at ‘http://localhost/ecommerce/public/api/login’ from origin ‘http://localhost:8100’ has been blocked by CORS policy

Could you please help.

image

I had issues sending my headers on my POST, my problem was very silly.
The structure of a POST is

POST(URL,BODY,OPTION?)
I was making something like http.post( url , headers).
Even if my body was empty I had to add it like http.post(url, '', headers);
My headers, in case you were wondering is in the form:

let headers: Headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('Authorization', `Bearer ${refresh_token}`);
2 Likes

Hello guys,
Did you solve this issue?
I am getting same error in Ionic 5 too.

The problem is that my Authorization token is never appended to the headers. When I traced using debugger, I found that reqOpts has my appended headers in something called lazyUpdate. My http call fails as no authorization token was sent so I get an Unauthorized error.

I doubt anybody will be able to say anything constructive without seeing enough of your code to reproduce the problem (and please make it available in a usable format, as opposed to screenshots of text).

Sharing the API service call details code.

import { HttpClient, HttpHeaders, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { ToastController, LoadingController } from '@ionic/angular';
import { Storage } from '@ionic/storage';
import { BalaEnv } from './bala-env-vars';
import { catchError } from 'rxjs/operators';
import { AlertController } from '@ionic/angular';
import { error } from 'util';


@Injectable({
  providedIn: 'root'
})

export class BalaService {
  auth_token: any;
  httpOptions: any;
  baseUrl: any;
  hasInit: boolean;
  userData: any;

  constructor(
    public http: HttpClient,
    private storage: Storage,
    private alertController: AlertController,
    private toastController: ToastController,
    private loadingCtrl: LoadingController,

  ) {
    this.hasInit = false;
    this.baseUrl = BalaEnv.BASE_API;
    this.init();
  }

  handleError(error: HttpErrorResponse) {
    let me = this;
    this.loadingCtrl.dismiss();
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
    } else {

      if (error.status == 401) {
        this.presentToast(error.error.detail);

        if (error.error.detail === 'Signature has expired.') {
          //  this.presentAlert(error.error.detail)
          me.storage.get('user')
            .then((user) => {
              me.recallToken(user).subscribe((res: any) => {
                me.storage.set('authToken', res.token);
                me.init();
              });
            });
        }
        else if (error.error.message != undefined) {
          me.presentToast(error.error.message)
        } else {
          return [];
        };
      } else if (error.status == 404) {
        me.presentToast('Link is broken');
      } else if (error.status == 400) {
        Object.keys(error.error).forEach(function (key) {
          me.presentToast(error.error[key][0])
        });
        return [];
      }
      else if (error.status == 500) {
        this.presentToast('something went wrong');
      }
    }
    return [];
  }
  async presentAlert(msg) {
    const alert = await this.alertController.create({
      header: 'Alert',
      message: msg,
      buttons: ['OK']
    });

    await alert.present();
  }
  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
  hasAuthToken() {
    return this.auth_token && true;
  }
  getAuthToken() {
    return this.auth_token;
  }
  getUser() {
    let t = this;
    t.storage.get("userDetails")
      .then((user) => {
      });
  }
  setAuthToekn(authToken: string) {
    this.auth_token = authToken;
  }
  setUserData(user: string) {
    this.userData = user;
  }
  getUserData() {
    return this.userData;
  }
  removeAuthToken() {
    this.auth_token = null;
  }
  refreshToken(token) {
    let t = this;
    let httpOption = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      })
    };
    return this.http.post(this.baseUrl + 'auth/jwt/refresh/', token, httpOption)
      .pipe(
        catchError((error) => {
          return t.handleError(error)
        })
      )
  }
  recallToken(payload) {
    let t = this;
    let httpOption = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      })
    };
    return this.http.post(this.baseUrl + 'auth/jwt/', payload, httpOption)
      .pipe(
        catchError((error) => {
          return t.handleError(error)
        })
      )
  }
  init() {
    let me = this;
    me.storage.get('authToken')
      .then((token) => {
        if (token != null) {
          me.hasInit = true;
          me.setAuthToekn(token);
          me.httpOptions = {
            headers: new HttpHeaders({
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'Authorization': 'JWT ' + token
            })
          };
        }
        else {
          me.httpOptions = {
            headers: new HttpHeaders({
              'Content-Type': 'application/json',
              'Accept': 'application/json',
            })
          };
        }
      });
  }
  setParams(params) {
    let obj = new HttpParams();
    for (let i in params) {
      obj = obj.set(i, params[i]);
    }
    return obj;
  }
  loginService(payLoad: any) {
    let me = this;
    return this.http.post(this.baseUrl + 'auth/', payLoad, this.httpOptions)
      .pipe(
        catchError((error) => {
          return me.handleError(error);
        })
      );
  }

  registrationService(payLoad: any) {
    let t = this;
    return this.http.post(this.baseUrl + 'auth/register/', payLoad, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      );
  }

  getLevel() {
    let t = this;
    return t.http.get(this.baseUrl + 'level', this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getLevelById(id) {
    let t = this;
    return t.http.get(this.baseUrl + 'level/' + id, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }

  //level/registration_submit/id/
  levelRegistration(u_id, payLoad) {
    let t = this;
    let httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'JWT ' + t.getAuthToken(),
        'Accept': 'application/json',
      })
    };
    return t.http.put(this.baseUrl + 'level/registration_submit/' + u_id, payLoad, httpOptions)
      .pipe(
        catchError((error) => {
          alert(JSON.parse(error));
          return t.handleError(error);
        })
      )
  }
//upload image / video in activity log
  uploadEventMedia(payLoad){
    let t = this;
    let httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'JWT ' + t.getAuthToken(),
        'Accept': 'application/json',
      })
    };
    return t.http.post(this.baseUrl + 'activity_log/activityuploads/', payLoad, httpOptions)
      .pipe(
        catchError((error) => {
          alert(JSON.parse(error));
          return t.handleError(error);
        })
      )
  }

  uploadImage(u_id, payLoad) {
    let t = this;
    let httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'JWT ' + t.getAuthToken(),
        'Content-Type': 'multipart/form-data',
        'Accept': 'application/json',
      })
    }; 
    return t.http.post(this.baseUrl + 'level/registration_submit_image/' + u_id, payLoad, httpOptions)
      .pipe(
        catchError((error) => {
          alert(JSON.parse(error));
          return t.handleError(error);
        })
      )
  }

  getState(payLoad) {
    let t = this;
    const requestparams = this.setParams(payLoad);
    const options = Object.assign({}, this.httpOptions, { params: requestparams });
    return t.http.get(this.baseUrl + 'location/states', options)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getCity(stateid) {
    let t = this;
    return t.http.get(this.baseUrl + 'location/cities/' + stateid, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getQuiz(lid) {
    let t = this;
    return t.http.get(this.baseUrl + 'level/quiz/' + lid, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getCallSlot(payLoad) {
    let t = this;
    const requestparams = this.setParams(payLoad);
    const options = Object.assign({}, this.httpOptions, { params: requestparams });
    return t.http.get(this.baseUrl + 'level/call_slots/', options)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  scheduleSkypeCall(payload) {
    let t = this;
    return t.http.post(this.baseUrl + 'level/accept_tc_coc/', payload, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  _addStandardHeaders(header: HttpHeaders, userId) {
    let t = this;
    header = header.set('Content-Type', 'application/x-www-form-urlencoded');
    header = header.set('Accept', 'application/json');
    header = header.set('Authorization', 'JWT ' + t.getAuthToken());
    header = header.set('User', userId);
    return header;
  }

  _initializeReqOpts(reqOpts) {
    if (!reqOpts) {
      reqOpts = {
        headers: new HttpHeaders(),
        params: new HttpParams()
      };
    }
    return reqOpts;
  }

  submitQuiz(payload) {
    let t = this;
    return t.http.post(this.baseUrl + 'level/quiz_result_submit/', payload, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  //upload video
  uploadVideo(id, payLoad) {
    let t = this;
    let httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'JWT ' + t.getAuthToken(),
        'Accept': 'application/json',
      })
    };
    return t.http.post(this.baseUrl + 'level/video_link_save_video/' + id, payLoad, httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }

  //upload video link
  uploadVideoLink(id, payLoad) {
    let t = this;
    return t.http.post(this.baseUrl + 'level/video_link_save_video/' + id, payLoad, t.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  //getFaqs
  getVideoLink(id) {
    let t = this;
    return t.http.get(this.baseUrl + 'level/video_link_save/' + id, t.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getFaqs(payLoad) {
    let t = this;
    const requestparams = this.setParams(payLoad);
    const options = Object.assign({}, this.httpOptions, { params: requestparams });
    return t.http.get(this.baseUrl + 'faq/', options)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }

  //getSpalshMessage

  getSpalshMessage() {
    let t = this;
    let httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      })
    };
    return t.http.get(this.baseUrl + 'general_settings/splash_msg/', httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }


  //meet and greet
  getMeetAndGreet() {
    let t = this;
    return t.http.get(this.baseUrl + 'team_member/', this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getTeamMember(id) {
    let t = this;
    return t.http.get(this.baseUrl + 'team_member/' + id, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  addActivityLog(payload){
let t=this;
return t.http.post(this.baseUrl+'activity_log/',payload,this.httpOptions)
.pipe(
  catchError((error)=>{
    return t.handleError(error);
  })
)
  }
  getAllActivityLog() {
    let t = this;
    return t.http.get(this.baseUrl + 'activity_log/all/', this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  getActivityLog(id) {
    let t = this;
    return t.http.get(this.baseUrl + 'activity_log/' + id, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  addweaverandrelease(payload) {
    let t = this;
    return t.http.post(this.baseUrl + 'level/weaverandrelease/', payload, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }

  addreleaseliability(payload) {
    let t = this;
    return t.http.post(this.baseUrl + 'level/releaseliability/', payload, this.httpOptions)
      .pipe(
        catchError((error) => {
          return t.handleError(error);
        })
      )
  }
  checkisbaalaboss(id) {
    let t = this;
    return t.http.get(this.baseUrl + 'level/isbaalaboss/' + id, this.httpOptions)
      .pipe(catchError((error) => {
        return t.handleError(error);
      })
      )
  }
}```


This format we used for header 
headers: new HttpHeaders({
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'Authorization': 'JWT ' + token
            })```

Just to eliminate two common sources of problems here:

  • is baseUrl an HTTPS endpoint, as opposed to bare HTTP? If not, does the problem persist even with HTTPS?
  • could this be a CORS problem? Authorization headers need to be explicitly permitted by the server

API end point is HTTPS.

But same code working fine in Android / Web platforms.

Do we need to enable “Access-Control-Allow-Headers” : “Authorization” in server.

I would refer you to MDN.