gpt4 book ai didi

c# - GET/DELETE 背后的原因不能在 webapi 中有正文

转载 作者:可可西里 更新时间:2023-11-01 08:46:42 25 4
gpt4 key购买 nike

为什么 HttpMethod,例如 GETDELETE 不能包含 body

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

同样在 Fiddler 中,如果我提供主体,背景会变成红色。但它仍然会在 body 上执行。

Fiddle Image

因此,作为替代方案,我使用了 SendAsync(),因为它接受可以包含 HttpMethod 以及 content 的 HttpRequestMessage

// 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

我是不是漏掉了什么?

最佳答案

根据 HTTP 标准,GET 方法旨在检索数据,因此无需提供请求主体。

添加请求正文违反了定义的规则。因此,这是被禁止的。

这同样适用于 DELETE 方法。

关于c# - GET/DELETE 背后的原因不能在 webapi 中有正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25783542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com