RestSharp更改"内容类型"改变来自" application / json" to" x-www-form-urlencoded"

时间:2017-04-13 05:00:00

标签: .net restsharp

问题:

1 - " Content-Type"改变来自" application / json"到" x-www-form-urlencoded"

2 - Task参数从对象{...}转换为字符串" {...}"

我看了这个: RestSharp defaulting Content-Type to application/x-www-form-urlencoded on POST 但是,没有解决我的问题。解决了问题1,但不是2.我不能保留对象参数。

我该如何解决这个问题?

代码:

  var parametros = new Dictionary<string, object>();

  parametros.Add("user", "23j7yh653eb7002e53df02");
  parametros.Add("client", "58e652dbe33eb7002e53df6f");
  parametros.Add("obs", "new one");

  var client = new RestClient("https://my.domain.com");
  var request = new RestRequest("api/newtask", Method.POST); 

  request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
  request.AddHeader("Content-Type", "application/json");
  request.AddHeader("Host", "my.domain.com");
  request.AddHeader("Referer", "https://my.domain.com/");
  request.AddHeader("Accept", "application/json, text/plain, */*");
  request.AddHeader("Accept-Encoding", "gzip, deflate, br");
  request.AddHeader("Authorization", "Bearer 85754r5565546544545566t55hf4f4j5k4fj45jk");

这一次我尝试了不同的方式:

  parametros.Add("task", "{ \"desc\" = \"This is my new task\", \"id\" = \"102030\" }");

或删除并放置:

  request.AddParameter("task",new {desc="This is my new task", id="102030"});

或者我试试这个:

  task myTask = new task();
  myTask.id = "102030";
  myTask.name = "This is my new task";

  request.AddParameter("task", Newtonsoft.Json.JsonConvert.SerializeObject(myTask));

然后

  foreach (var item in parametros)
  {
        //request.AddParameter(item.Key.ToString(), item.Value, ParameterType.RequestBody);  //I tried this one, but don't worked
        request.AddParameter(item.Key.ToString(), item.Value);
  }

  IRestResponse response2 = client.Post(request);
  string appoint = response2.Content;

1 个答案:

答案 0 :(得分:0)

我不知道这是否可以帮助某人。那就是解决方案。

就是这样:

将参数添加到词典:

     parametros.Add("task", myTask);

只需添加一个请求参数:

    request.AddParameter("application/json",
         Newtonsoft.Json.JsonConvert.SerializeObject(parametros),
         ParameterType.RequestBody);

整个代码:

  task myTask = new task();
  myTask.id = "102030";
  myTask.name = "This is my new task";

  var parametros = new Dictionary<string, object>();

  parametros.Add("user", "23j7yh653eb7002e53df02");
  parametros.Add("client", "58e652dbe33eb7002e53df6f");
  parametros.Add("obs", "new one");
  parametros.Add("task", myTask);

  var client = new RestClient("https://my.domain.com");
  var request = new RestRequest("api/newtask", Method.POST); 

  request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
  request.AddHeader("Content-Type", "application/json");
  request.AddHeader("Host", "my.domain.com");
  request.AddHeader("Referer", "https://my.domain.com/");
  request.AddHeader("Accept", "application/json, text/plain, */*");
  request.AddHeader("Accept-Encoding", "gzip, deflate, br");
  request.AddHeader("Authorization", "Bearer 85754r5565546544545566t55hf4f4j5k4fj45jk");

    request.AddParameter("application/json",
         Newtonsoft.Json.JsonConvert.SerializeObject(parametros),
         ParameterType.RequestBody);

  IRestResponse response2 = client.Post(request);
  string appoint = response2.Content;