简体   繁体   中英

RestSharp - GET request with parameters

I have a problem with sending request with parameters. I have example of sending the request in PHP, but I can't figure out, how should it look like in RestSharp:

在此处输入图片说明

As you can see, in the example parameters are added to private key (which I have also done) and then also there's this CURLOPT_POSTFIELDS where params are added too. I tried doing it by AddParameter, AddBody, AddJsonBody and nothing works. When I have parameters concatenated to private key, response is always empty, if I remove it, I get the response, but my parameters are ignored.

        string data = "{\"Paging\":{\"per_page\":\"" + 10 + "\"}}";

        RestClient client = new RestClient("api");

        string header = "WMS " + publicKey + ":" + GetMd5Hash(privateKey + data);

        IRestRequest request = new RestRequest("products", Method.GET);
        request.AddHeader("Content-Type", "application/json; charset=utf-8");
        request.AddHeader("Authorization", header);
        request.AddHeader("Content-Length", data.Length.ToString());

        //request.RequestFormat = RestSharp.DataFormat.Json;
        request.AddParameter("Paging", new { per_page = 10 });

        IRestResponse response = client.Execute(request);

        Encoding encoding = Encoding.GetEncoding("utf-8");
        var result = encoding.GetString(response.RawBytes);

I was able to track the php request with fiddler and raw request looked like that:

GET api HTTP/1.1
Host: api
Pragma: no-cache
Accept: */*
Authorization: WMS md5
Content-type: application/json
Content-Length: 27

{"Paging":{"per_page":"8"}}

And mine looks like that:

GET api HTTP/1.1
Authorization: WMS md5
Content-Type: application/json
Accept: */*
User-Agent: RestSharp/105.2.3.0
Host: api
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

The parameter isn't shown in it, don't know why. I tried every parameter type. Aside from that my header "Content-Length" is not visible either.

Adding parameters to a GET request, is done just using:

request.AddParameter("name", "value");

I think you have other problems in your code, probably mixing json format, but as we don't have the details of the actual API you are consuming we can't advice with it.

May be you could simplify the example, test it, and then add more config as the API shows you errors. Check the basic example here: https://github.com/restsharp/RestSharp

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