Body of Http.DELETE request in Angular2

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

    In Angular Http 7, the DELETE method accepts as a second parameter options object in which you provide the request parameters as params object along with the headers object. This is different than Angular6.

    See example:

    this.httpClient.delete('https://api-url', {
        headers: {},
        params: {
            'param1': paramValue1,
            'param2': paramValue2
        }
    });
    
    0 讨论(0)
  • 2020-12-02 09:20

    Below is a relevant code example for Angular 4/5 with the new HttpClient.

    import { HttpClient } from '@angular/common/http';
    import { HttpHeaders } from '@angular/common/http';
    
    public removeItem(item) {
        let options = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
          }),
          body: item,
        };
    
        return this._http
          .delete('/api/menu-items', options)
          .map((response: Response) => response)
          .toPromise()
          .catch(this.handleError);
      }
    
    0 讨论(0)
  • 2020-12-02 09:23
    deleteInsurance(insuranceId: any) {
        const insuranceData = {
          id : insuranceId
        }
        var reqHeader = new HttpHeaders({
                "Content-Type": "application/json",
            });
            const httpOptions = {
                headers: reqHeader,
                body: insuranceData,
            };
        return this.http.delete<any>(this.url + "users/insurance", httpOptions);
        }
    
    0 讨论(0)
  • 2020-12-02 09:27

    Below is the relevant code example for Angular 2/4/5 projects:

    let headers = new Headers({
      'Content-Type': 'application/json'
    });
    
    let options = new RequestOptions({
      headers: headers,
      body: {
        id: 123
      }
    });
    
    return this.http.delete("http//delete.example.com/delete", options)
      .map((response: Response) => {
        return response.json()
      })
      .catch(err => {
        return err;
      });
    

    Notice that body is passed through RequestOptions

    0 讨论(0)
  • 2020-12-02 09:29

    Definition in http.js from the @angular/http:

    delete(url, options)

    The request doesn't accept a body so it seem your only option is to but your data in the URI.

    I found another topic with references to correspond RFC, among other things: How to pass data in the ajax DELETE request other than headers

    0 讨论(0)
  • 2020-12-02 09:29

    If you use Angular 6 we can put body in http.request method.

    Reference from github

    You can try this, for me it works.

    import { HttpClient } from '@angular/common/http';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss'],
    })
    export class AppComponent {
    
      constructor(
        private http: HttpClient
      ) {
        http.request('delete', url, {body: body}).subscribe();
      }
    }
    
    0 讨论(0)
提交回复
热议问题