Angular5中如何通过HttpClient Delete请求传递请求体?
Ah, I get it—you're running into trouble because you're putting your request body in the wrong parameter. Let's break down what's wrong and how to fix it.
The Problem in Your Code
You're passing your removeFile body to the observe parameter, which is totally not what that's for. The observe parameter is meant to specify what part of the HTTP response you want to handle (like 'body' for just the response data, or 'response' for the full HTTP response object). Shoving your request body here is why you're seeing errors.
The Correct Implementation
In Angular 5's HttpClient, the DELETE method accepts the request body as part of the options object, under the body property. Here's how to adjust your code:
let body = removeFile; return this.httpClient.delete(`${apiRoot}RemoveQueryData`, { headers: new HttpHeaders() .set('Content-Type', 'application/json') .set('Authorization', `Bearer ${accessToken}`), body: body, // This is where your request body belongs observe: 'body' // Use 'response' instead if you need the full HTTP response details });
A Quick Heads-Up
While Angular supports sending a body with DELETE requests, not all backend frameworks or servers handle this by default. Double-check that your backend endpoint is configured to accept a body alongside DELETE requests if you still run into issues after fixing the frontend code.
内容的提问来源于stack exchange,提问作者siva.kumar

