Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason behind GET/DELETE cannot have body in webapi

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?

like image 716
Pedigree Avatar asked Sep 11 '14 09:09

Pedigree


People also ask

Can a delete API request have a body?

According to Mozilla a DELETE request "may" have a body, compared to a PUT, which should have a body. By this it seems optional whether you want to provide a body for a DELETE request.

Can we send body in HTTP delete request?

Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters. This is usually an ID of the resource you want to delete.

Should delete response have a body?

The short answer is: You should include a response body with an entity describing the deleted item/resource if you return 200.


1 Answers

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.

like image 67
Julien Jacobs Avatar answered Oct 11 '22 11:10

Julien Jacobs