Sending body in HttpClient delete method (Angular 5)

自闭症网瘾萝莉.ら 提交于 2020-12-07 05:18:12

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/48685569/sending-body-in-httpclient-delete-method-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!