0

I have been using restsharp to make this simple request to a webservice:

    string sParmDb = Newtonsoft.Json.JsonConvert.SerializeObject(parmsDb);// parmsDb is some object too large to be passed as a query parameter
        sParmDb = System.Web.HttpUtility.UrlEncode(sParmDb);

        RestSharp.RestRequest;
        RestSharp.RestClient cl = new RestSharp.RestClient();
        RestSharp.RestResponse rsp;

        string sQueryStr = "http://myWebService";

        restCall = new RestSharp.RestRequest(sQueryStr, RestSharp.Method.POST);
        restCall.AddJsonBody(sParmDb);

        rsp = (RestSharp.RestResponse)cl.Execute(restCall);

this works fine on NuGet version 106.9.0 but when I updated the package to 108.0.1 it doesn't compile. RestSharp.Method doesn't contain POST, though it does have a Post, so perhaps that's a simple change.

The main problem is that RestRequest no longer contains AddJsonBody.

What would be the simplest, quickest code change that will fix this? Thanks for your help.

UPDATE 26/7/2022 ---------------- I realized that the reason that restCall.AddJsonBody was not available was that I needed to include the RestSharp.Extensions as a using directive. Now this code compiles under version 108, the current version:

        RestSharp.RestClient cl = new RestSharp.RestClient();
        RestSharp.RestRequest restCall = new RestSharp.RestRequest("http://localhost:60484/api/db", RestSharp.Method.POST);
        restCall.AddJsonBody("abcdefg");
        RestSharp.RestResponse rsp = (RestSharp.RestResponse)cl.Execute(restCall);

This reaches the web service method, which I am running in debug mode on localhost, but the string value parameter is null. Do I need another line(s) of code in the calling client to make it work as it does under version 106? The web service looks like this:

    public string Post([FromBody] string value)
    {
    //do something with value and return some string
}

1 Answers1

0
string sQueryStr = "http://myWebService";
 RestSharp.RestClient cl = new RestSharp.RestClient(sQueryStr);
 var restCall = new RestRequest() {  Method=  Method.Post};
 ...
 restCall.AddStringBody(sParmDb, RestSharp.ContentType.Json);

article https://restsharp.dev/usage.html#request-body

Mihal By
  • 162
  • 2
  • 12
  • The problem is that in version 107.0.1, the current release, RestRequest does not have methods AddJsonBody and AddStringBody. It has AddParameter. Am I missing a using directive or assembly reference? – polymechan Jul 24 '22 at 21:20
  • my typo: current release is 108.0.1 – polymechan Jul 25 '22 at 06:25
  • @polymechan In versions than107.X AddJsonBody exists, AddStringBody not exists. I think is not a missing derectives or assemblies. If can't update to 108, try use AddParameter or AddObject extensions. In my case request parameters is keypairs - before version 108 used code bellow: `foreach (var key in parameters) { request.AddParameter(key, parameters[key]); }` – Mihal By Jul 25 '22 at 06:37