英文:
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<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.
答案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<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})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论