Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. public async Task<HttpResponseMessage> CallApiJsonAsync(HttpMethod method, string relativeUrl, object content)
  2.         {
  3.             var request = new HttpRequestMessage(method, relativeUrl);
  4.             if (content != null)
  5.             {
  6.                 var json = JsonConvert.SerializeObject(content);
  7.                 request.Content = new StringContent(json, Encoding.UTF8, "application/json");
  8.             }
  9.             return await _client.SendAsync(request);
  10.         }
  11.  
  12.         public async Task<TResult> CallApiJsonAsync<TResult>(HttpMethod method, string relativeUrl, object content) where TResult : class
  13.         {
  14.             var response = await CallApiJsonAsync(method, relativeUrl, content);
  15.             if (response.IsSuccessStatusCode)
  16.             {
  17.                 var responseContent = await response.Content.ReadAsStringAsync();
  18.                 return JsonConvert.DeserializeObject<TResult>(responseContent);
  19.             }
  20.             return null;
  21.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement