Home > OS >  HTTP response before the request is complete
HTTP response before the request is complete

Time:05-27

I have built a controller that has to make a number of http calls to assemble response. Using postman to test. As the controller goes through the first call, postman receives a response back. I am completely at a loss to understand the flow:

[HttpGet("")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public async Task<IActionResult> GetPackageAsync()
{
  var filepackagesresp = aClient.GetPackageDetailsAsync();
  return Ok(filepackagesresp.ToString());
}

This controller is calling the following method

public async Task<Package> GetPackageDetailsAsync()
{
var packageresp = await httpClient.GetAsync(serviceurl);
// At this point POSTMAN receives a response

packageresp.EnsureSuccessStatusCode();
var packageResponse = JsonConvert.DeserializeObject<GetPackageResponse>
    (await packageresp.Content.ReadAsStringAsync());

var packagepropertiesresp = await httpClient.GetAsync(serviceurl);
.....
there are more calls like this
......

Postman shows 200 OK and the following in the response which

"System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1 AsyncStateMachineBox`1[namespace.Package,System.Runtime.CompilerServices.IAsyncStateMachine]"

Is there any explanation of this behavior and what can I do to debug this? Advance thanks to anyone who looks into it

CodePudding user response:

Currently, the controller is converting the task to a string and returning that. Your controller should await the task, which extracts the result object, and then return that result:

public async Task<IActionResult> GetPackageAsync()
{
  var filepackagesresp = await aClient.GetPackageDetailsAsync();
  return Ok(filepackagesresp.ToString());
}
  • Related