Passing body content when calling a Delete Web API method using System.Net.Http

前端 未结 3 1604
刺人心
刺人心 2020-12-20 16:36

I have a scenario where I need to call my Web API Delete method constructed like the following:

// DELETE: api/products/{id}/headers
[HttpDelete(\"{id}/heade         


        
3条回答
  •  独厮守ぢ
    2020-12-20 16:57

    I think the reason HttpClient is designed that way is although HTTP 1.1 spec allows message body on DELETE requests, essentially it is not expected to do so as the spec doesn't define any semantics for it as it is defined here. HttpClient strictly follows HTTP spec thus you see it doesn't allow you to add a message body to the request.

    So, I think your option from the client side includes using HttpRequestMessage described in here. If you want to fix it from the backend and if your message body would work well in query params you can try that instead of sending the query in message body.

    I personally think DELETE should be allowed to have a message body and should not be ignored in a server as there are certainly use cases for that like the one you mentioned here.

    In any case for more productive discussion on this please have a look at this.

提交回复
热议问题