Set #39;Content-Type#39; header using RestSharp(使用 RestSharp 设置“Content-Type标头)
问题描述
我正在为 RSS 阅读服务构建客户端.我正在使用 RestSharp 库与他们的 API 进行交互.
I'm building a client for an RSS reading service. I'm using the RestSharp library to interact with their API.
API 声明:
创建或更新记录时,您必须将 application/json;charset=utf-8
设置为 Content-Type
标头.
When creating or updating a record you must set
application/json;charset=utf-8
as theContent-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);
但是;服务返回错误
错误 415:请使用 'Content-Type: application/json;charset=utf-8' 标题
Error 415: Please use the 'Content-Type: application/json; charset=utf-8' header
为什么 RestSharp 不传递标头?
Why isn't RestSharp passing the header?
推荐答案
我的博客 在 RestSharp 1.02 版本之后没有经过测试.如果您针对我的解决方案的具体问题提交评论,我可以更新它.
The solution provided on my blog is not tested beyond version 1.02 of RestSharp. If you submit a comment on my answer with your specific issue with my solution, I can update it.
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);
这篇关于使用 RestSharp 设置“Content-Type"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 RestSharp 设置“Content-Type"标头


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01