简体   繁体   中英

RestSharp Array Request

Send array post RestSharp? Dictionary, List<> is not compatibile ?

public class Test
{
    public string key { get; set; }
    public string viewType { get; set; }
    public string module { get; set; }
    public string method { get; set; }

    public Dictionary<string, string> parameters { get; set; }
}

My class init.

Test t = new Test();

            t.key = "xxxxxxxxxxxxxxxxxx";
            t.viewType = "json";
            t.module = "test";
            t.method = "test";

            t.parameters = new Dictionary<string,string>();
            t.parameters.Add("p1", "data1");

Send data request

 IRestResponse response = restClient.Execute<Test>(restRequest);

Send is debbuger:

[JSON]
  -request
       module: "test"
       method: "test"
       parameters:"System.Collections.Generic.Dictionary`2[System.String,System.String]"

Who RestSharp create ? ? Send Array options object ?

$postData = array(
  'key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'viewType' => 'json',
  'module' => 'test',
  'method' => 'test',
  [options] => stdClass Object
  (
    [name] => 'ADIDAS'
  )
);

I found this in the documentation:

To add all properties for an object as parameters, use AddObject(). To add a file for upload, use AddFile() (request will be sent as multipart encoded form). To include a request body (like XML or JSON), use AddBody();

So because you are using restRequest.AddObject() , RestSharp uses the value of t.parameters.ToString() instead of serializing to JSON.

Fix: use restRequest.AddBody(t) instead. You also have to specify the content type.

request.RequestFormat = DataFormat.Json;
request.AddBody(t);
RestClient restClient = new RestClient("https:");

            RestRequest restRequest = new RestRequest(Method.POST);

            Test t = new Test();

            t.key = ac.Password;
            t.viewType = "json";
            t.module = "hello";
            t.method = "hello";

            t.parameters = new Dictionary<string,string>();
            t.parameters.Add("wddwdw", "ddd");


            restRequest.AddObject(t);

 IRestResponse response = restClient.Execute<Test>(restRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM