Body of Http.DELETE request in Angular2

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

    Since the deprecation of RequestOptions, sending data as body in a DELETE request is not supported.

    If you look at the definition of DELETE, it looks like this:

        delete(url: string, options?: {
          headers?: HttpHeaders | {
             [header: string]: string | string[];
            };
          observe?: 'body';
          params?: HttpParams | {
              [param: string]: string | string[];
             };
          reportProgress?: boolean;
          responseType?: 'json';
          withCredentials?: boolean;
         }): Observable;
    

    You can send payload along with the DELETE request as part of the params in the options object as follows:

    this.http.delete('http://testAPI:3000/stuff', { params: {
        data: yourData
         }).subscribe((data)=>. 
            {console.log(data)});
    

    However, note that params only accept data as string or string[] so you will not be able to send your own interface data unless you stringify it.

提交回复
热议问题