gpt4 book ai didi

c# - 使用 RestSharp 设置 'Content-Type' header

转载 作者:IT王子 更新时间:2023-10-29 04:10:05 29 4
gpt4 key购买 nike

我正在为 RSS 阅读服务构建一个客户端。我正在使用 RestSharp库与他们的 API 进行交互。

API 声明:

When creating or updating a record you must set application/json;charset=utf-8 as the Content-Type header.

我的代码是这样的:

RestRequest request = new RestRequest("/v2/starred_entries.json", Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddParameter("starred_entries", id);

//Pass the request to the RestSharp client
Messagebox.Show(rest.ExecuteAsPost(request, "POST").Content);

但是;服务返回错误

Error 415: Please use the 'Content-Type: application/json; charset=utf-8' header

为什么 RestSharp 不传递 header ?

最佳答案

my blog 上提供的解决方案未测试超过 RestSharp 1.02 的版本。如果您针对我的解决方案的具体问题提交对我的回答的评论,我可以更新它。

var client = new RestClient("http://www.example.com/where/else?key=value");
var request = new RestRequest();

request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);

var response = client.Execute(request);

关于c# - 使用 RestSharp 设置 'Content-Type' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17815065/

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