How to call a DELETE REST API with body by using HttpClient in ASP.NET Core
Dung Do Tien Dec 30 2020 498
I have an issue when trying to call HTTP delete with some information inside of the request body.
I'm using HttpClient
class and using DeleteAsync
method but I can't push anything into the body
Here is my code. I can call delete request but I can't attach body in my delete request.
HttpClient http = new HttpClient();
var result = await http.DeleteAsync("https://localhost:44313/api/products/1");
Do you have any ideas, guys?
Have 1 answer(s) found.
- M0
Marry Christ Dec 30 2020
DeleteAsync
method build-in function ofHttpClient
hasn't allowed you to push anything into your request.You can use
SendAsync
and define type is Delete instead ofDeleteAsync
Here is an example:
HttpClient http = new HttpClient(); var httpMessage = new HttpRequestMessage(HttpMethod.Delete, "YOUR_URL_HERE") { Content = new StringContent("YOURDATA", Encoding.UTF8, "application/json") }; var result = await http.SendAsync(httpMessage);
I hope it will help you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.