gpt4 book ai didi

c# - 如何添加 "Content-Type = application/json;v=2.0",包括使用 HttpClient 进行版本控制?

转载 作者:行者123 更新时间:2023-12-05 06:27:01 24 4
gpt4 key购买 nike

不好意思问这样的问题。我可以给你一种感觉,我希望你为我编写代码。我已经花了一天时间编写代码来发送包含带有版本控制的 header 的 http 请求:

对于版本控制,我使用 versioning by Media Type来自 Microsoft/aspnet-api-versioning 项目。

我的 API 部分工作正常,我可以使用 Postman 毫无问题地请求正确的版本:

enter image description here

你看到这一行“Content-Type = application/json;v=2.0”了吗?在 postman 上没问题。使用 C# 中的 HttpClient 是不可能的。

让我在这里复制所有我尝试过但出现错误的解决方案。每次尝试新的解决方案时,我都会编辑这个问题。通过设施,但为了更清楚,我将复制粘贴图像:

解决方案 1:具有新 Content = ... 的扩展方法

enter image description here

解决方案 1B:来自初学者Tejas

enter image description here

解决方案 1C:来自 BeginnerTejas 但现在使用 MediaTypeWithQualityHeaderValue

enter image description here

解决方案 2:带有 request.Headers.Add(...) 的扩展方法

enter image description here

解决方案 3:带有 Client.DefaultRequestHeaders.TryAddWithoutValidation(...) 的 BaseProxy看来我无法添加任何“Content-Type”

enter image description here

解决方案 4:带有 Client.DefaultRequestHeaders.Accept.Add(...) 的 BaseProxy

enter image description here

解决方案 5:request.Headers.TryAddWithoutValidation("Content-Type", "application/json");

enter image description here

最佳答案

你能试试这个吗?

request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue( "application/json" )
{
Parameters = { new NameValueHeaderValue( "v", "2.0" ) }
};

关于c# - 如何添加 "Content-Type = application/json;v=2.0",包括使用 HttpClient 进行版本控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685234/

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