azure function c# httpclient getasync receives bad request at initial run

KANNIBALA MISKIN KANNISAH 31 Reputation points
2023-09-22T06:20:37.9933333+00:00

Hi

I was using HttpClient to call one of the API's. When I deployed to Azure function (durable function), it gets failed for the error, Bad Request.

When I retrigger it manually, it works fine. Means, it gets failed for initial run and subsequent runs were working fine. Please help me to fix this issue. Below is the snippet of my code.

 using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Clear();
                    string responseBody = "";
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apimKey);
                    client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jwtToken}");
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    log.LogInformation("Request Call Initiated...");

                    var stringTask = client.GetAsync(apiUrl.AbsoluteUri);

                    HttpResponseMessage response = await stringTask.ConfigureAwait(false) ?? throw new HttpRequestException($"The GET method resquest did not return a body");
                    responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (string.IsNullOrEmpty(responseBody)) throw new HttpRequestException($"The GET method returned an empty/null body");

                    log.LogInformation("Response code: " + response.StatusCode.ToString());

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        client.CancelPendingRequests();
                        log.LogInformation("Received response from Api...");
                        string responseContent = await response.Content.ReadAsStringAsync();
                        return responseContent;
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        var error = $"The Uri returned was BadRequest (400), HTTPResposeBody: " + response.Content?.ReadAsStringAsync().Result;
                        log.LogInformation(error);
                        throw new HttpRequestException(error);
                    }
                    else
                    {
                        log.LogError("The GET call did NOT respond with a 200OK! ==> Current response: " + response.StatusCode.ToString() + " HTTPResposeBody: " + response.Content?.ReadAsStringAsync());
                        throw new Exception("The GET call did NOT respond with a 200OK! ==> Current response: " + response.StatusCode.ToString() + " HTTPResposeBody: " + response.Content?.ReadAsStringAsync());
                    }

                };
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,867 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.