Search code examples
c#jsonrestrestsharp

Get JSON response using RestSharp


I'm new to C# and I'm trying to get the JSON response from a REST request using RestSharp; The request I want to execute is the following one : "http://myurl.com/api/getCatalog?token=saga001". It works great if I'm executing it in a browser.

I've tried this :

var client = new RestClient("http://myurl.com/api/");

var request = new RestRequest("getCatalog?token=saga001"); 

var queryResult = client.Execute(request);

Console.WriteLine(queryResult);

And I get "RestSharp.RestReponse" instead of the JSON result I'm hopping for.

Thanks for your help !


Solution

  • Try:

    var client = new RestClient("http://myurl.com/api/");
    
    var request = new RestRequest("getCatalog?token={token}", Method.GET); 
    
    request.AddParameter("token", "saga001", ParameterType.UrlSegment);   
    
    // request.AddUrlSegment("token", "saga001"); 
    
    request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
    
    var queryResult = client.Execute(request);
    
    Console.WriteLine(queryResult.Content);