1

I want to update part of data through an API. I've successfully made a POST data. I would also like to do PATCH to update one field of the data.

Below is the code am trying to use for the changes:

                else if (out_timing.worker_timeout_id === emp_id && out_timing.check_status === false) {
              let checkOutTime = new TimeOutModel(out_timing.date, emp_id, out_timing.time_out, true);
              //introduced two lines below to carry out update for last field
              let updateCheckStatus = new TimeInModel("", "", "", false);
              this.employeesService.patchTimeinStatus(updateCheckStatus)
              this.employeesService.pushTimeOut(checkOutTime)
                .subscribe(checkOutTime => {
                },
                error => this.errorMessage = <any>error);
              console.log("Successfully checked-out!");
              break;
            }
            else {
              console.log("All conditions exhausted!");
              break;
            }

This is what I have in my service component.

patchTimeinStatus(timing: TimeInModel): Observable<TimeInModel> {

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.patch(this.empApiUrl + '/time-in/', timing, options)
      .map(this.extractData)
      .catch(this.handleErrorObservable);
}

I get 405 method not allowed from the server. Apart from that, I doubt whether what am doing is correct. What is the correct approach to do the update using patch or put methods?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
7guyo
  • 3,047
  • 1
  • 30
  • 31
  • More about PUT, PATCH you can find here - https://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples. In your case if you want update just part of the data the best way is use the PATCH method. – Alisher Gafurov Jun 06 '17 at 10:34

0 Answers0