12

Why do HttpMethods such as GET and DELETE cannot contain body?

public Task<HttpResponseMessage> GetAsync(Uri requestUri);
public Task<HttpResponseMessage> DeleteAsync(string requestUri);

also in Fiddler, if I supply a body, the background turns red. But still it will execute with body on it.

Fiddle Image

So as an alternative, I used SendAsync() because it accepts HttpRequestMessage which can contain HttpMethod as well as the content.

// other codes
Category category = new Category(){ Description = "something" };
string categoryContent = JsonConvert.SerializeObject(category);
string type = "application/json";

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Delete, "-page-")
HttpContent content = new StringContent(categoryContent, Encoding.UTF8, type);
HttpClient client = new HttpClient();

message.Content = content;
await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
// other codes

Did I missed something else?

Pedigree
  • 2,384
  • 3
  • 23
  • 28
  • 1
    Maybe these two articles could help you. [RESTful Alternatives to DELETE Request Body](http://stackoverflow.com/questions/14323716/restful-alternatives-to-delete-request-body) and [Is an entity body allowed for an HTTP DELETE request?](http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request) – John Woo Sep 11 '14 at 09:40
  • 2
    Is there any special reason why you want to send a body with a GET or DELETE request? In general, there is no reason to use a body in these type of requests. – Horizon_Net Sep 11 '14 at 11:05

1 Answers1

9

Per HTTP standards, GET method is aimed to retrieve data, hence there is no need to provide a request body.

Adding a request body violates the rules defined. This is therefore prohibited.

The same applies to DELETE method.

Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34