Body of Http.DELETE request in Angular2

前端 未结 12 1987
旧巷少年郎
旧巷少年郎 2020-12-02 08:23

I\'m trying to talk to a somewhat RESTful API from an Angular 2 frontend.

To remove some item from a collection, I need to send some other data in addition to the re

12条回答
  •  死守一世寂寞
    2020-12-02 09:10

    You are actually able to fool Angular2 HTTP into sending a body with a DELETE by using the request method. This is how:

    let body = {
        target: targetId,
        subset: "fruits",
        reason: "rotten"
    };
    
    let options = new RequestOptionsArgs({ 
        body: body,
        method: RequestMethod.Delete
      });
    
    this.http.request('http://testAPI:3000/stuff', options)
        .subscribe((ok)=>{console.log(ok)});
    

    Note, you will have to set the request method in the RequestOptionsArgs and not in http.request's alternative first parameter Request. That for some reason yields the same result as using http.delete

    I hope this helps and that I am not to late. I think the angular guys are wrong here to not allow a body to be passed with delete, even though it is discouraged.

提交回复
热议问题