Tauqir
Tauqir

Reputation: 491

HTTP response before the request is complete

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

Upvotes: 0

Views: 657

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456587

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());
}

Upvotes: 1

Related Questions