Passing headers in HTTP.get

Hi
I’m working on a project and I’m dealing with JWT, anyway I have tried to send post requests and it went as a charm but when I tried to make get requests the same way is not working.
here is my code :

ionViewWillEnter(){

  this.storage.get('token').then((val) => {
     this.token = val 
  
  console.log(this.token);
  let link = Constants.API_ENDPOINT+"/v1/owner/section/list";
  console.log("Debugging");
  console.log(link);

  this.http.get(link,{},{Authorization: "Bearer "+val})
    .then(data =>{
      var hdrs = JSON.parse(data.headers);
      var res = JSON.parse(data.data);
      
      console.log(res);
      console.log(hdrs)
    })
    .catch(error =>{
      console.log("Yup Errors area ")
      console.log(error.status);
    console.log(error.error); // error message as string
    console.log(error.headers);
    
  });
  });
}

I get the following output :

 Debugging
 (the correct link)
 Yup Errors area ["a message I have set to make sure that the code executed the catch method"]
undefined
undefined
undefined

any idea why am I getting this errors ?

Note :

I have sent a request to the same link with the same headers in Postman and it returned the correct response

for the undefined errors it was because I have tried to parse the headers which are not json.

I would urge anybody coming across this thread to structure their code differently. Pages should not be interacting directly with HTTP. They should be treating service providers as black boxes that deliver data in the format they want. This gives you much more flexibility, testability, and futureproofing. All interaction with HTTP should be in the providers.

3 Likes