如何将ID数组作为DELETE API的请求体传递?

huangapple go评论78阅读模式
英文:

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

问题

我的DELETE API在Swagger上接受一个要删除的id数组,格式如下:

[
  42
]

我在Angular v15中有以下代码,但我不知道如何将id数组作为请求体传递给这个DELETE API。

deleteProduct(productId: number): Observable<any> {
    let idArray = JSON.parse("[2023]");
    return this.http
        .delete(this.baseUrl + "api/Product/deleteproduct", idArray)
        .pipe(map((data) => data));
}

上述代码会报错:

媒体类型不支持

我已经搜索了很多东西,但是没有找到解决方案。

英文:

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&lt;any&gt; {

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

    return this.http

      .delete(this.baseUrl + &quot;api/Product/deleteproduct&quot;, frequencyArray)

      .pipe(map((data) =&gt; data));

  }

Above code is giving an error:

> Media type unsupported

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

答案1

得分: 0

从HttpClient类中:

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>;

所以你需要在选项数组中传递body数据。

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

英文:

From the HttpClient class:

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

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

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

huangapple
  • 本文由 发表于 2023年7月27日 17:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定