Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular Httpclient post doesn't work, but i can't catch any exception

So im trying to to make a POST with the http client to my rest api. I only have to send an ID, but it dosen’t do anything at all, as if it wasn’t there. No error messages etc.

Tried it this way:

httpOptions = {

    headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type")+" "+localStorage.getItem("token")})
  };

 public likePet(id : string){
    var url = environment.apiKey+"/match/cr";
    const params = new HttpParams()
      .set('id', id)

    console.log(url);
    try{
      console.log("t1");
      this.httpClient.post(url,{params: params},this.httpOptions);
    } catch (error){
      console.error(error);
    }
    console.log("t2");
 }

and that way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 httpOptions = {
    headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type")+" "+localStorage.getItem("token")})
  };

public createMatch(matchid: number){
  console.log("////");
  return this.httpClient.post(environment.apiKey+"match/cr?id="+matchid,this.httpOptions).pipe(
    catchError((err) => {
      console.error(err);
      window.alert("Failed");
      throw err
    })
  )
}

Tried it with a complete hardcoded url like: localhost:8080/api/match/cr?id=1

And not even an error message. API working fine. My other http GET/POST methods working.
But this is my first POST without sending a JSON and im clueless.

>Solution :

The post() method returns an Observable. As far as I am concerned the call is sent as soon as you subscribe to that Observable. Wherever you call createMatch() try createMatch(matchId).subscribe() and see if something happens.

If you don’t want to work with Observables but would rather like your response as a Promise you could write

 httpOptions = {
    headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type")+" "+localStorage.getItem("token")})
  };

public createMatch(matchid: number){
  console.log("////");
  return this.httpClient.post(environment.apiKey+"match/cr?id="+matchid,this.httpOptions).pipe(
    catchError((err) => {
      console.error(err);
      window.alert("Failed");
      throw err
    })
  ).toPromise()
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading