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

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

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

问题

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

  1. [
  2. 42
  3. ]

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

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

上述代码会报错:

媒体类型不支持

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

英文:

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

  1. [
  2. 42
  3. ]

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.

  1. deleteProduct(productId: number): Observable&lt;any&gt; {
  2. let frequencyArray = JSON.parse(&quot;[2023]&quot;);
  3. return this.http
  4. .delete(this.baseUrl + &quot;api/Product/deleteproduct&quot;, frequencyArray)
  5. .pipe(map((data) =&gt; data));
  6. }

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类中:

  1. delete<T>(url: string, options?: {
  2. headers?: HttpHeaders | {
  3. [header: string]: string | string[];
  4. };
  5. context?: HttpContext;
  6. observe?: 'body';
  7. params?: HttpParams | {
  8. [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
  9. };
  10. reportProgress?: boolean;
  11. responseType?: 'json';
  12. withCredentials?: boolean;
  13. body?: any | null;
  14. }): Observable<T>;

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

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

英文:

From the HttpClient class:

  1. delete&lt;T&gt;(url: string, options?: {
  2. headers?: HttpHeaders | {
  3. [header: string]: string | string[];
  4. };
  5. context?: HttpContext;
  6. observe?: &#39;body&#39;;
  7. params?: HttpParams | {
  8. [param: string]: string | number | boolean | ReadonlyArray&lt;string | number | boolean&gt;;
  9. };
  10. reportProgress?: boolean;
  11. responseType?: &#39;json&#39;;
  12. withCredentials?: boolean;
  13. body?: any | null;
  14. }): 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:

确定