Skip to main content

Posts

Showing posts from November, 2022

Generic function to call Web API

  public class RestClient     {         public static async Task<T> CallServiceAsync<T>(string token, string endPoint, string url, object requestBodyObject, string method, string operation = null) where T : class         {             if (token == null)                 throw new Exception("Please provide token");             // Initialize an HttpWebRequest for the current URL.             var webReq = (HttpWebRequest)WebRequest.Create(endPoint + url);             webReq.Method = method;             webReq.Accept = "application/json";             //Add basic authentication header if username is supplied             if (!string.IsNullOrEmpty(token))                 webReq.Headers["Authorization"] = "Bearer " + token;             webReq.Headers["Content-Type"] = "application/json";             //Add key to header if operation is supplied             if (!string.IsNullOrEmpty(operation))                 webReq.Headers