Reputation: 15
I am new to writing generic methods in C# and I am trying to create a generic SendAsync
method in my c# project. Code is given below.
HttpClientHelper.cs
public static async Task<T2t> ExecuteRequest<T1,T2>(T2 request, string url)
{
Uri requestUri = new Uri(url);
string payload = JsonConvert.SerializeObject(request);
HttpContent httpContent = new StringContent(payload, "application/json");
HttpRequestMessage requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = requestUri,
Content = httpContent
};
return await SendUriAsync<T2>(requestMessage);
}
public static async Task<T> SendUriAsync<T>(HttpRequestMessage requestMessage)
{
string clint_id = "1234";
var clineCred = Encoding.UTF8.GetBytes($"{client_id}");
using(var result = await client.SendAsync(requestMessage))
{
result.EnsureSuccessStatusCode();
var response = await result.Content.ReadAsStringAsync();
if(result.IsSuccessStatusCode && response != null)
{
return JsonConvert.DeserializeObject<T>(response);
}
else
{
return null;
}
}
}
Here is the controller class which calls these methods:
CarSalesController.cs
string thirdpartyUrl = "someurl";
var responseObject = await HttpClientHelper.ExecuteRequest<CarObject, string>(requestObject, thirdpartyUrl);
I am getting error in the HttpClientHelper
class. The error is:
ResponseStatusCode does not indicate success. StatusCode:401 - UnAuthorised
But the same API works well when I use postman. He re is the cURL
curl --location --request GET 'someurl'
--header 'client_id:1234'
--header 'Authorization: Basic asdf'
--header 'Content-Type: application/json'
--data-raw '{
"data1":"somedata1",
"data2":"somedata2"
}'
What could be wrong in my code?
Upvotes: 0
Views: 1834
Reputation: 360
Isn't this what you are trying to do?
public async Task<TOut> ExecuteRequestAsync<TIn, TOut>( TIn request, string url )
{
var requestUri = new Uri( url );
var payload = JsonConvert.SerializeObject( request );
var httpContent = new StringContent( payload, Encoding.UTF8, "application/json" );
var requestMessage = new HttpRequestMessage()
{
Method = HttpMethod.Post,
RequestUri = requestUri,
Content = httpContent
};
return await SendUriAsync<TOut>( requestMessage );
}
public static async Task<T> SendUriAsync<T>( HttpRequestMessage requestMessage )
{
using (var client = CreateClient( 1234 ))
{
var result = await client.SendAsync( requestMessage );
result.EnsureSuccessStatusCode();
var response = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>( response );
}
}
private static HttpClient CreateClient(int clientId)
{
var clientCred = Encoding.UTF8.GetBytes( clientId.ToString() );
var auth = Convert.ToBase64String( clientCred );
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", auth );
client.DefaultRequestHeaders.Add( "client_id", "1234" );
return client;
}
Upvotes: 2
Reputation: 11
The problem might be with the DeserializeObject
. Try using:
response.Content.ReadAsStringAsync()
Upvotes: 1