0

How can i manage exceptions from Client.PostAsync ?

I have this code:

            AzureDevOpsSearchApiResponse searchResults = await client.PostAsync<AzureDevOpsSearchApiResponse>
            (SiteGlobal.AzureDevOpsSearchRestApi, searchRequest.AsJson());

but i may not have status code "200" or maybe there was other issue. how can i manage & handle the exception that may appear from the response?

maor yadin
  • 51
  • 8
  • Can you clarify what you're asking here? Are you asking how to catch exceptions? And what is `client`? – ProgrammingLlama May 26 '20 at 09:17
  • 1
    @JohnathanBarclay It doesn't appear to be `HttpClient` since `PostAsync` is a generic method here. – ProgrammingLlama May 26 '20 at 09:21
  • Yes, i am trying to send http post request. everything work well and i get answer back. but i want to catch any exception from it like i have with HttpResponseMessage: EnsureSuccessStatusCode() // Throw exception if not a success code. – maor yadin May 26 '20 at 09:21
  • 1
    [Does this answer your question](https://stackoverflow.com/questions/19034199/properly-handling-httpclient-exceptions-within-async-await) ? You can find the exceptions it can throw [in the docs](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.postasync?view=netcore-3.1) – ProgrammingLlama May 26 '20 at 09:22
  • Yes i have a generic function: public virtual async Task PostAsync(string requestUrl, HttpContent content) { HttpResponseMessage response = await client.PostAsync(requestUrl, content); var t = response.Content.ReadAsStringAsync(); return await response.Content.ReadAsAsync(); } @John – maor yadin May 26 '20 at 09:25
  • Ok i have succeeded to resolve this thanks ;) – maor yadin May 26 '20 at 09:27

0 Answers0