Sending Authorization in header


#1

Hi,
I am trying to send token in web API call , but for some reason I am getting error 401 unauthorised. I tried sending same request using fiddler and it worked fine. see the code snippet below:

return new Promise((resolve, reject) => {
      let headers = new HttpHeaders();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      headers.append('Accept', 'application/json');
      headers.append('Authorization', 'Bearer TY3k5TnQsBvVygnZh4vA5TEEyCnH8EaJQEUNCsrf7BC2IoYxmNTMsZDOnlerITBh-_4mkNWgWOgKqPDdnkOc5tpQ2IXTZd-ibkjTw79tQK-YGsX0f0VjuFwpDkFqnmQlBFAdaAtqlYzXLwgKGvP5M6wgeBvPxjZUrRTZWFMqyHoqOlpRV4Z3BgKTjCS0vWftJoOUyIx0Rh2K2yHqUu2J43qLV9QfFNXb-cVqoftQPPtKXtj8vzmxWGk42dp9zLvQT_LsIF6fx3H6nXgxbZ-NqWcuF7OmoHD2dwXQgjgnMhF-e3CGv2uQ-lkh2nedA85WWaajStxQLMDTwG65e2BA1SLISSu1GdEajROg7CQtx5YpOrPCXZ_FlMGediCUP9YJAVw64aLNTrzfvMM1K0bHCsJfkoNdpysIRhTYk_po4_YEkfg17SilMRN4ULNWUb8KxtoWv12vjpD_4YCDErbHmEDQNkkMwuK-ahG8Wt1LjGbI0bzoj2gD2gj7u-EzcDni'); 
  
      this.http.get(`${this.baseUrl}/ProductCategory`, {headers: headers})
        .subscribe(res => {           
          resolve(res);          
        }, (err) => {
          reject(err);
        });

Any idea what I am doing wrong here.

Thanks


#2
return new Promise((resolve, reject) => {
      let headers = new HttpHeaders();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      headers.append('Accept', 'application/json');
      headers.append('Authorization', 'Bearer TY3k5TnQsBvVygnZh4vA5TEEyCnH8EaJQEUNCsrf7BC2IoYxmNTMsZDOnlerITBh-_4mkNWgWOgKqPDdnkOc5tpQ2IXTZd-ibkjTw79tQK-YGsX0f0VjuFwpDkFqnmQlBFAdaAtqlYzXLwgKGvP5M6wgeBvPxjZUrRTZWFMqyHoqOlpRV4Z3BgKTjCS0vWftJoOUyIx0Rh2K2yHqUu2J43qLV9QfFNXb-cVqoftQPPtKXtj8vzmxWGk42dp9zLvQT_LsIF6fx3H6nXgxbZ-NqWcuF7OmoHD2dwXQgjgnMhF-e3CGv2uQ-lkh2nedA85WWaajStxQLMDTwG65e2BA1SLISSu1GdEajROg7CQtx5YpOrPCXZ_FlMGediCUP9YJAVw64aLNTrzfvMM1K0bHCsJfkoNdpysIRhTYk_po4_YEkfg17SilMRN4ULNWUb8KxtoWv12vjpD_4YCDErbHmEDQNkkMwuK-ahG8Wt1LjGbI0bzoj2gD2gj7u-EzcDni'); 
  
      this.http.get(`${this.baseUrl}/ProductCategory`, {}, {headers: headers})
        .subscribe(res => {           
          resolve(res);          
        }, (err) => {
          reject(err);
        });

#3

Hi,
Thanks for your reply , I am using HttpClient not Http. the get method takes 2 parameters.
Thanks


#4
let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Accept', 'application/json');
    headers.append('Authorization', 'Bearer TY3k5TnQsBvVygnZh4vA5TEEyCnH8EaJQEUNCsrf7BC2IoYxmNTMsZDOnlerITBh-_4mkNWgWOgKqPDdnkOc5tpQ2IXTZd-ibkjTw79tQK-YGsX0f0VjuFwpDkFqnmQlBFAdaAtqlYzXLwgKGvP5M6wgeBvPxjZUrRTZWFMqyHoqOlpRV4Z3BgKTjCS0vWftJoOUyIx0Rh2K2yHqUu2J43qLV9QfFNXb-cVqoftQPPtKXtj8vzmxWGk42dp9zLvQT_LsIF6fx3H6nXgxbZ-NqWcuF7OmoHD2dwXQgjgnMhF-e3CGv2uQ-lkh2nedA85WWaajStxQLMDTwG65e2BA1SLISSu1GdEajROg7CQtx5YpOrPCXZ_FlMGediCUP9YJAVw64aLNTrzfvMM1K0bHCsJfkoNdpysIRhTYk_po4_YEkfg17SilMRN4ULNWUb8KxtoWv12vjpD_4YCDErbHmEDQNkkMwuK-ahG8Wt1LjGbI0bzoj2gD2gj7u-EzcDni');

    let options = new RequestOptions({ headers: headers });

this.http.get(`${this.baseUrl}/ProductCategory`, options)
     .subscribe(res => {           
          resolve(res);          
        }, (err) => {
          reject(err);
        });