Get JSON response using RestSharp

Adrien Budet picture Adrien Budet · Apr 15, 2014 · Viewed 40.7k times · Source

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 !

Answer

chridam picture chridam · Apr 15, 2014

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);