I have a client-side and server-side project both working on my localhost in .net 8. When i try to send a post request to my server-side i get error System.Threading.Tasks.TaskCanceledException I wrote many more controllers until i get this error, those requests were working fine but this one… Also when i try to send the same request on Postman it works fine. Here is my server-side code:
[HttpPost]
public async Task<IActionResult> Create([FromBody] Book book)
{
var result = await _unitOfWork.Books.Add(book);
if (result)
{
await _unitOfWork.SaveAsync();
return Ok(book);
}
return BadRequest();
}
My client-side:
public async Task<Book?> CreateBook(Book book)
{
string url = baseUrl + "api/Books/Create";
HttpResponseMessage response = await _httpClient.PostAsJsonAsync(url, book);
return null;
}
It is returnin null etc. for now i am just trying to solve the issue. What I tried so far is:
I expanded httpclient timeout.
I added the same headers as in postman request.
I enabled cors and set it up.
I was getting the same exception when I try to send a Get request, then I added some headers to it, I tried it on postman and I added the headers i saw then it worked but that post request does not work. Here were the headers I added:
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");