C# 使用 RestSharp 设置“Content-Type”标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17815065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:33:07  来源:igfitidea点击:

Set 'Content-Type' header using RestSharp

c#http-headersrestsharp

提问by Shane Gowland

I'm building a client for an RSS reading service. I'm using the RestSharplibrary to interact with their API.

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

The API states:

API 指出:

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

创建或更新记录时,您必须将其设置application/json;charset=utf-8Content-Type标题。

This is what my code looks like:

这是我的代码的样子:

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);

However; the service is returning an error

然而; 服务返回错误

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

错误 415:请使用“内容类型:应用程序/json;charset=utf-8' 标头

Why isn't RestSharp passing the header?

为什么 RestSharp 不传递标题?

采纳答案by Itanex

The solution provided on my blogis 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.

我博客上提供的解决方案没有经过 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);

回答by D.R.

回答by Riju Vashisht

Here is the solution

这是解决方案

http://restsharp.blogspot.ca/

http://restsharp.blogspot.ca/

Create a json object with same name properties and set the values (Make sure they are similar to those of name value pair for post request.)

创建一个具有相同名称属性的 json 对象并设置值(确保它们与 post 请求的名称值对相似。)

After that use default httpclient.

之后使用默认的httpclient。

回答by Marc Wittmann

Although this is a bit old: I ran into the same problem.. seems some attributes such as "content-type" or "date" cannot be added as parameter but are added internally. To alter the value of "content-type" I had to change the serialzer setting (altough I didn`t use it because I added a json in the body that was serialized before!)

虽然这有点旧:我遇到了同样的问题..似乎某些属性(例如“内容类型”或“日期”)不能作为参数添加,而是在内部添加。要更改“内容类型”的值,我必须更改序列化器设置(虽然我没有使用它,因为我在之前序列化的主体中添加了一个 json!)

RestClient client = new RestClient(requURI);
RestRequest request = new RestRequest(reqPath, method);
request.JsonSerializer.ContentType = "application/json; charset=utf-8";

as soon as I did this the header showed up as intended:

一旦我这样做,标题就会按预期显示:

 System.Net Information: 0 : [5620] ConnectStream#61150033 -   Header 
 {
  Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
  User-Agent: RestSharp 104.1.0.0
  Content-Type: application/json; charset=utf-8
  ...
 }

回答by Tom Elmore

In version 105.2.3.0 I can solve the problem this way:

在 105.2.3.0 版本中,我可以这样解决问题:

var client = new RestClient("https://www.example.com");
var request = new RestRequest("api/v1/records", Method.POST);
request.AddJsonBody(new { id = 1, name = "record 1" });
var response = client.Execute(request);

Old question but still top of my search - adding for completeness.

老问题,但仍然是我搜索的首要问题 - 添加完整性。