using System.Threading.Tasks;
public static void Main()
Console.WriteLine(ExecuteRestApi("https://postman-echo.com/get", "GET", @"{""Authorization"":""Bearer asdfkjhgaskdjcbkaseyr""}", "").Result);
public static async Task<string> ExecuteRestApi(string url, string method, string headers, string body)
Console.WriteLine($"Start of the REST API execution {url}, {method}, {headers}, {body}");
var baseUri = uri.GetLeftPart(UriPartial.Authority);
var options = new RestClientOptions(baseUri) {ThrowOnAnyError = true, MaxTimeout = -1 };
var client = new RestClient(options);
var request = new RestRequest();
request.Method = (Method)Enum.Parse(typeof(Method), method);
if (!string.IsNullOrWhiteSpace(body))
request.AddParameter("application/json", body, RestSharp.ParameterType.RequestBody);
if (!string.IsNullOrWhiteSpace(headers))
var headersObj = System.Text.Json.Nodes.JsonNode.Parse(headers);
foreach (var kv in headersObj.AsObject())
request.AddHeader(kv.Key, kv.Value.ToString());
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful)
Console.WriteLine($"Failed to execute the REST API request {url}, {method}, {headers}, {body}");
Console.WriteLine($"Failed to execute the REST API with exception. {url}, {method}, {headers}, {body} {ex.Message}");
Console.WriteLine($"End of the REST API execution {url}, {method}, {headers}, {body}");