Created
June 17, 2019 11:30
-
-
Save faridprogrammer/211ca5894993ce5ba52e47bbcacec33a to your computer and use it in GitHub Desktop.
Simple http helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class HttpHelper | |
{ | |
public static async Task Post<T>(string baseUrl, string url, T contentValue) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseUrl); | |
var content = new StringContent(JsonConvert.SerializeObject(contentValue), Encoding.UTF8, "application/json"); | |
var result = await client.PostAsync(url, content); | |
result.EnsureSuccessStatusCode(); | |
} | |
} | |
public static async Task Put<T>(string baseUrl, string url, T stringValue) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseUrl); | |
var content = new StringContent(JsonConvert.SerializeObject(stringValue), Encoding.UTF8, "application/json"); | |
var result = await client.PutAsync(url, content); | |
result.EnsureSuccessStatusCode(); | |
} | |
} | |
public static async Task<T> Get<T>(string baseUrl, string url) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseUrl); | |
var result = await client.GetAsync(url); | |
result.EnsureSuccessStatusCode(); | |
string resultContentString = await result.Content.ReadAsStringAsync(); | |
T resultContent = JsonConvert.DeserializeObject<T>(resultContentString); | |
return resultContent; | |
} | |
} | |
public static async Task Delete(string baseUrl, string url) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseUrl); | |
var result = await client.DeleteAsync(url); | |
result.EnsureSuccessStatusCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment