-1

I'm dealing with an endpoint that someone else wrote a long time ago that the return looks like this:

return Ok();

On my side, with most of the other endpoints, I know how to deal with the return values. Like with a boolean return, I'll do this:

bool result = httpClient.Post<bool>(this.Uri, request).Result;

But with that empty Ok(), I'm not sure how to handle it on my side. Any hints would be most appreciated.

dave reid
  • 39
  • 3
  • 3
    What http client are you using? – Achtung Jul 25 '20 at 16:03
  • Ok means http status code 200, so if its 200 then use the return value – Vivek Nuna Jul 25 '20 at 16:12
  • You're *not* handling the response correctly. There's no `HttpClient.Post`. Is that your own method? At the very least you should `await` it, not block it. [HttpClient.PostAsync](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.postasync?view=netcore-3.1#System_Net_Http_HttpClient_PostAsync_System_Uri_System_Net_Http_HttpContent_System_Threading_CancellationToken_) returns an `HttpResponseMessage` with a Status code and the response content. Your method probably checks that status code already and returns the payload if it's OK, otherwise it throws – Panagiotis Kanavos Jul 25 '20 at 16:21
  • It could be Angular's HttpClient, which has that signature. – Noah Stahl Jul 25 '20 at 16:29

1 Answers1

1

In ASP.NET Core, an Ok() returns an empty 200 response. You could handle such using the HttpClient:

var downloadResult = await httpClient.GetAsync(uri);
if (downloadResult.IsSuccessStatusCode)
{
    // Handle success
}
Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
  • I think it's a POST, not a GET – Julian Jul 25 '20 at 16:17
  • In that case, simply use PostAsync instead. Same handling if empty response. – Noah Stahl Jul 25 '20 at 16:20
  • @Julian it doesn't matter, both `HttpClient.PostAsync` and `GetAsync` return an `HttpResponseMessage`. There's no `HttpClient.Post`, this is either the OP's own method or some extension with an unfortunate name. The OP (or the author of the extension method) *already* handles the status code one way or another – Panagiotis Kanavos Jul 25 '20 at 16:23
  • It would make you example a bit better, but sure there isn't a big difference – Julian Jul 25 '20 at 17:49