How to call a DELETE REST API with body by using HttpClient in ASP.NET Core

Dung Do Tien Dec 30 2020 332

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.
  • M

    Marry Christ Dec 30 2020

    DeleteAsync method build-in function of HttpClient hasn't allowed you to push anything into your request.

    You can use SendAsync and define type is Delete instead of DeleteAsync

    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.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close