I am using HttpClient for get and post request to my WebAPI. But I'm using generic Get.
My problem is how can I bind T generic object to GetAsync method?
Example, in my POST request I can use generic T like this:
public async Task<TResponse> Post<TRequest, TResponse>(TRequest request, string urlToSend)
{
try
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(_url);
var serialized = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var httpResponseMessage = await httpClient.PostAsync(urlToSend, serialized);
//httpResponseMessage.EnsureSuccessStatusCode();
if (httpResponseMessage.IsSuccessStatusCode)
{
//var responseResultRaw = httpResponseMessage.Content.ReadAsStringAsync().Result;
var responseString = JsonConvert.DeserializeObject<TResponse>(httpResponseMessage.Content.ReadAsStringAsync().Result);
return responseString;
}
else /*if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.BadRequest)*/
{
var errorRes = JsonConvert.DeserializeObject<TResponse>(httpResponseMessage.Content.ReadAsStringAsync().Result);
return errorRes;
}
throw new Exception(httpResponseMessage.ReasonPhrase);
}
}
catch (Exception e)
{
return default;
}
}
But i did not found any example of how can I insert my generic TRequest to GetAsync().
Here is my code:
public async Task<TResponse> GetByFilter(TRequest request, string urlToRequest)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(_url);
var response = await client.GetAsync(urlToRequest);
var responseString = await response.Content.ReadAsStringAsync();
var responseJson = JsonConvert.DeserializeObject<TResponse>(responseString);
return responseJson;
}
throw new NotImplementedException();
}
Simply, my question is can i use a model in GetAsync() method like httpClient.PostAsync(urlToSend, serialized);
My API controller method is like this:
[HttpGet("getfirmdetail")]
public IActionResult GetFirmDetail([FromBody]FirmDetailRequestDto firmDetailRequestDto)
{
return BadRequest(firmDetailRequestDto);
}
CodePudding user response:
With a Get request there is no body to put the serialized 'request' in. You could put the serialized 'request' in a querystring parameter but there are limitations to the total length of an Url. If you can, always use Post.
CodePudding user response:
The GET requests doesn't have a body, so you have to pass all the parameters in the URL, in your example the attribute urlToRequest.
Otherwise if you are using NET Core you can go with:
public async Task<TResponse> GetByFilter(TRequest request, string urlToRequest)
{
using (HttpClient client = new HttpClient())
{
var jsonBody= Newtonsoft.Json.JsonConvert.SerializeObject(request)
var requestGet = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(urlToRequest),
Content = new StringContent(jsonBody, Encoding.UTF8, ContentType.Json),
};
var resp = await client.SendAsync(requestGet).ConfigureAwait(false);
var responseString = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseJson = JsonConvert.DeserializeObject<TResponse>(responseString);
return responseJson;
}
}