Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,867 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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());
}
};