背后原因GET / DELETE不能有身体的WebAPI [英] Reason behind GET/DELETE cannot have body in webapi

查看:214
本文介绍了背后原因GET / DELETE不能有身体的WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么列举HTTPMethod 取值如 GET 删除不能包含体内

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.

因此​​,作为一种替代方法,我用 SendAsync(),因为它接受的Htt prequestMessage 可包含列举HTTPMethod 还有的内容的。

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?

推荐答案

每HTTP标准,GET方法的目的是获取数据,因此没有必要提供一个请求主体。

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.

这同样适用于DELETE方法。

The same applies to DELETE method.

这篇关于背后原因GET / DELETE不能有身体的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆