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 put and post wont work

I have multiple services in my Angular Ionic App all copys of each other with the slightest changes

I have a method like this (delete/update/get all work):

endpoint = 'https://localhost:7183/api/CheckinData';
httpOptions = {headers: new HttpHeaders({ 'content-Type': 'application/json' })};

clockIn(time, date)
  {
    const reply = new CheckinData();
    reply.userid = 2; //placeholder untill better authentication implementation
    reply.time = time;
    reply.date = date;
    this.create(reply);
  }

create(data: CheckinData): Observable<any>
  {
    console.log('clockin', this.endpoint, JSON.stringify(data));
    return this.http.post(this.endpoint, JSON.stringify(data), this.httpOptions)
      .pipe(catchError(this.handleError<CheckinData>('Error occured')));

  }

idk if its relevant but here is my CheckinData class

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

export class CheckinData
{
  id: number;
  time: Time;
  date: Date;
  userid: number;
}

Console log works with return (-> method gets called):
clockin https://localhost:7183/api/CheckinData {"userid":2,"time":"10 45 AM","date":"15 11 2022"}

There are no Errors

But when I call get/ check in SSMS there is no new Entry

For example this call works just fine (different service, different Api controller)

  create(todo: ToDo): Observable<any> {
    console.log('createToDo', this.endpoint, JSON.stringify(todo));
    return this.httpClient.post(this.endpoint, JSON.stringify(todo), this.httpOptions)
      .pipe(catchError(this.handleError<ToDo>('Error occured')));
  }

I can post with swagger/postman (-> api works)

I tried put instead of post and it doesn’t work either

-> there has to be another potential Mistake Iam Missing

If you have any Ideas what could be wrong/what else to check all Help is appreciated

>Solution :

From your code, I don’t see your front-end side will submit the request to API. You need to subscribe the Observable in order to submit the request.

clockIn(time, date) {
  ...

  this.create(reply)
    .subscribe(x => {
      // Action after receive response from API
    });
}
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