Skip to content

Instantly share code, notes, and snippets.

@jchannon
Last active March 29, 2017 14:10
Show Gist options
  • Save jchannon/8103d1127e3e00d8e3be33e467bae147 to your computer and use it in GitHub Desktop.
Save jchannon/8103d1127e3e00d8e3be33e467bae147 to your computer and use it in GitHub Desktop.
public static class HttpClientExtensions
{
public static T Get<T>(this HttpClient client, string url)
{
var response = client.GetAsync(url).Result;
var content = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(content);
}
public static HttpResponseMessage PostAsJson(this HttpClient client, string url)
{
return client.PostAsJson(url, new { });
}
public static HttpResponseMessage PostAsJson(this HttpClient client, string url, object model)
{
return client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json")).Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment