Search code examples
arraysangularswaggerangular15

How to pass an array of IDs as a body in DELETE API?


My DELETE API on swagger is accepting an array of ids to delete. Like this:

[
  42
]

I have following code in angular v15. I am not getting how to pass an array of ids as a body to this delete API.

deleteProduct(productId: number): Observable<any> {

    let frequencyArray = JSON.parse("[2023]");

    return this.http

      .delete(this.baseUrl + "api/Product/deleteproduct", frequencyArray)

      .pipe(map((data) => data));

  }

Above code is giving an error:

Media type unsupported

I have searched a lot of things, but I am not getting any solution.


Solution

  • From the HttpClient class:

    delete<T>(url: string, options?: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        context?: HttpContext;
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
        };
        reportProgress?: boolean;
        responseType?: 'json';
        withCredentials?: boolean;
        body?: any | null;
    }): Observable<T>;
    

    so you need to pass the body data in the option array.

    Example .delete(this.baseUrl + "api/Product/deleteproduct", {body: frequencyArray})