5

Could Angular2 send json body via http method delete ?

I try it and it said error

ORIGINAL EXCEPTION: options.search.clone is not a function

service.ts

deletetag(tagid: number): Observable<any> {

    let body = JSON.stringify(
        {
            "token": "test",
            "content": {
                "tagId": tagid
            }
        }
    );

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.delete("http://localhost:8080/backend/tag", body, options)
        .map(res => this.extractData(res))
        .catch(this.handleError);
}

component.ts

this.tagService.deletetag(1)
   .subscribe(
      data => { },
      error => { },
      () => { }
   );
Mahalo Bankupu
  • 272
  • 3
  • 5
  • 19

1 Answers1

10

As per RequestOptionsArgs interface and Http delete function argument, i think you need to send delete request as below :

deletetag(tagid: number): Observable<any> {

  let body = JSON.stringify(
      {
        "token": "test",
        "content": {
        "tagId": tagid
      }
    }
  );
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({
    headers: headers,
    body : body
  });

  return this.http.delete("http://localhost:8080/backend/tag", options)
        .map(res => this.extractData(res))
        .catch(this.handleError);
}
ranakrunal9
  • 13,320
  • 3
  • 42
  • 43
  • Although this may work in angular, you may have issues server side with this: https://stackoverflow.com/a/25237188/3806701 – cs_pupil Sep 21 '17 at 17:18
  • in Angular 5, the RequestOptions is DEPRECATED, see https://angular.io/api/http/RequestOptions – Nam Le Jan 08 '18 at 08:39