Generic request with Rest Sharp
May 6, 2016
1 minute read

RestSharp is a simple ‘REST and HTTP API Client for .NET’. The MakeRequest function highlighted below takes a relative endpoint, makes a request and deserializes to the model type that is passed into the RestClient Execute function.

public static IRestResponse<T> MakeRequest(string relativeUrl, Func<RestClient, RestRequest, IRestResponse<T>> clientFunc)
    var client = new RestClient("");
    var request = new RestSharp.RestRequest(relativeUrl);
    return clientFunc(client, request);

// example call
var request = MakeRequest("/api/whatever", (c, r) => c.Execute<SomeType>(r));
var deserializedSomeTypeData = request.Data;

Back to index

comments powered by Disqus