Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Http delete send json in body

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 => { },
      () => { }
   );
like image 888
Mahalo Bankupu Avatar asked Sep 21 '16 05:09

Mahalo Bankupu


People also ask

Can we send body in HTTP delete request?

The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters.

Can we send payload in delete request angular?

You can send payload along with the DELETE request as part of the params in the options object as follows: this. http. delete('http://testAPI:3000/stuff', { params: { data: yourData }).

Should HTTP delete return a body?

The short answer is: You should include a response body with an entity describing the deleted item/resource if you return 200.


1 Answers

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);
}
like image 153
ranakrunal9 Avatar answered Oct 20 '22 04:10

ranakrunal9