18

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 !

chridam
  • 100,957
  • 23
  • 236
  • 235
Adrien Budet
  • 327
  • 2
  • 3
  • 13

4 Answers4

25

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);
chridam
  • 100,957
  • 23
  • 236
  • 235
  • I got the same result. – Adrien Budet Apr 15 '14 at 14:45
  • I've tested it and the result is the same again, it's really weird – Adrien Budet Apr 15 '14 at 14:57
  • If you debug your application, what properties does your `queryResult RestSharp.RestReponse` object expose? I suppose you could write to console either the `Content` or `Data` properties. – chridam Apr 15 '14 at 15:04
  • 2
    Writing to console the Content property was the solution, thank you :) – Adrien Budet Apr 15 '14 at 15:13
  • 1
    `request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };` did the job for me. The problem was, that the content type of the response was `text/html`. I guess that RestSharp then tries to use XML deserialization – neutron Dec 06 '18 at 13:24
6

This is old but I was just struggling with this too. This is the easiest way I found.

var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest("getCatalog?token=saga001"); 
var response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
{
    // Two ways to get the result:
    string rawResponse = response.Content;
    MyClass myClass = new JsonDeserializer().Deserialize<MyClass>(response);
}
Aximili
  • 28,626
  • 56
  • 157
  • 216
4

Try as below:

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

client.ClearHandlers();
var jsonDeserializer = new JsonDeserializer();
client.AddHandler("application/json", jsonDeserializer);

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

var queryResult = client.Execute(request);

Console.WriteLine(queryResult);
aaron ngo
  • 41
  • 5
1

If you want to save the result into JSON file: You should use these namespaces:

using RestSharp;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/json");
var queryResult = client.Execute<Object>(request).Data;
string json = JsonConvert.SerializeObject(queryResult);
System.IO.File.WriteAllText(@"C:\...\path.json", json);
MeirDayan
  • 620
  • 5
  • 20