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:
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.
CodePudding user response:
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()
}