Body of Http.DELETE request in Angular2

前端 未结 12 2003
旧巷少年郎
旧巷少年郎 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:05

    The http.delete(url, options) does accept a body. You just need to put it within the options object.

    http.delete('/api/something', new RequestOptions({
       headers: headers,
       body: anyObject
    }))
    

    Reference options interface: https://angular.io/api/http/RequestOptions

    UPDATE:

    The above snippet only work for Angular 2.x, 4.x and 5.x.

    For versions 6.x and 7.x, Angular offers different overloads for what you need. Check all overloads here: https://angular.io/api/common/http/HttpClient#delete

    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
      }),
      body: {
        id: 1,
        name: 'test',
      },
    };
    
    this.httpClient
      .delete('http://localhost:8080/something', options)
      .subscribe((s) => {
        console.log(s);
      });
    

提交回复
热议问题