4

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:

enter image description here

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.

Sir Rufo
  • 16,671
  • 2
  • 33
  • 66
Dess
  • 345
  • 3
  • 7
  • 19
  • 1
    Use [Fiddler](https://www.telerik.com/fiddler) to log the real request send by php. – Sir Rufo Nov 02 '17 at 11:55
  • Fiddler is not registering this request :/ – Dess Nov 02 '17 at 12:08
  • I managed to track it so I updated my question – Dess Nov 02 '17 at 12:45
  • 1
    Just as a guess: RestSharp is not able to do this. As an explanation why read https://stackoverflow.com/a/983458/1744164 – Sir Rufo Nov 02 '17 at 13:45
  • Can someone suggest any library which I could use as a subtitute? – Dess Nov 02 '17 at 14:23
  • FYI also the .net classes will prevent you from sending content on GET: https://github.com/Microsoft/referencesource/blob/master/System/net/System/Net/HttpWebRequest.cs#L1250 - any library build on this will fail too – Sir Rufo Nov 02 '17 at 16:13

1 Answers1

6

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

Romias
  • 12,977
  • 7
  • 51
  • 79