http://www.c-sharpcorner.com/members/hamid-khan9
private async Task<HttpResponseMessage> SendToApi<T>(string httpType, string endpoint, T payload, string accessToken = null)
{
using (var client = new HttpClient())
{
var baseUrl="http://localhost:8080";
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
else if (!string.IsNullOrEmpty(accessToken))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",accessToken);
}
HttpResponseMessage apiResponse = null;
try
{
switch (httpType)
{
case "Get":
apiResponse = await client.GetAsync(endpoint);
break;
case "Post":
apiResponse = await client.PostAsJsonAsync(endpoint, payload);
break;
case "Put":
apiResponse = await client.PutAsJsonAsync(endpoint, payload);
break;
case "Delete":
apiResponse = await client.DeleteAsync(endpoint);
break;
}
}
catch (HttpRequestException ex)
{
// log the ex using logger so we have record of it
new MyCustomLogger().Log(ex);
}
return apiResponse;
}
}
No comments:
Post a Comment