Angular 7 Http delete api handle body?

user10851063 picture user10851063 · Jan 3, 2019 · Viewed 14.3k times · Source

I'm trying to talk to a somewhat REST API from an Angular 7 front end.

To remove some item from a collection, I need to send some other data in addition to the remove unique id, namely an authentication token, some collection info and some ancillary data.

However, the Http module of Angular 7 doesn't quite approve of a DELETE request with a body, and trying to make this request.

Here is my api:

DELETE /user/verifications

body {
  "doc_type": "govt_id",
  "doc_id": "5beedd169db947867b710afd"
}

Answer

ravi picture ravi · Jan 3, 2019

this will work for angular 6+, where http is your HttpClient

const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      }),
      body: {
        name: 'ravi',
        id: 'ravi123'
      }
    }

    this.http.delete('http://localhost:8080/user', options).subscribe(s => {
      console.log(s);
    })