Fel
Fel

Reputation: 4828

Sending body in HttpClient delete method (Angular 5)

I'm migrating Http to HttpClient in an Angular 5 application and everything went fine until I got to this method of one of the services:

deleteMultipleObjects(userId: number, officeId : number, objectsData : any) {
  const url = `${APP_URL}/delete-objects/${userId}/${officeId}`;

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

  this.appOptionsService.log("Deleting multiple objects.....", url, objectsData);

  return this.http
    .delete(url, { headers : headers, body : objectsData } )
    .map(this.extractData)
    .catch(this.handleError);
}

As you can see, I pass an object objectsData in the body of the delete call that basically has this structure:

{
  objectIds: [id1, id2, id3, ...]
}

I see that the delete method in the HttpClient doesn't allow to include a body, so how could I send the array of object ids to delete with the call? I have a lot of delete calls that are the same as this one, so I'll have to change a lot of things in the code to adapt it...

Thanks!

Upvotes: 2

Views: 11106

Answers (1)

Gregor Doroschenko
Gregor Doroschenko

Reputation: 11696

You can not send body in DELETE http request in Angular.

HTTP specification on MDN says that this possible. documentation page.

If you need this functionality, you should use PUT as workaround. Another method to allow multiple e.g. id's on your delete API endpoint. For example:

https://server.com/api/delete?ids=1,2,3,4,5

That requires some changes on your api.

Upvotes: 1

Related Questions