Azure Function app API call with HttpRequestMessage

136 views Asked by At

I have an Azure function app that calls a Web API using System.Net.Http.HttpRequestMessage.

The URLs were changed from their side so I had to update all different paths.

I updated the URLs, republished the Azure function app and now I get this error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (myapi.com:443)

Inner exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Here is the code that calls the API to obtain the token:

public static async Task<string> GetApiConnectionToken(HttpClient client, ILogger log)
{
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

    var dict = new Dictionary<string, string>
        {
            { "grant_type", "client_credentials" },
            { "client_secret", "client_secret" },
            { "client_id", "GUID" },
            { "audience", "https://myapi.com/id/" }
        };

    log.LogInformation("Cert bypass applied");

    var url = @"https://myapi.com/id/getToken";
    var req = new HttpRequestMessage(HttpMethod.Post, url) 
                  { Content = new FormUrlEncodedContent(dict) };

    var res = await client.SendAsync(req);

    log.LogInformation("Token received");

    var accessKey = JsonConvert.DeserializeObject<ApiCredentials>(await res.Content.ReadAsStringAsync());

    return accessKey.access_token;
}

If I try via localhost via VS 2022, it is able to obtain the token.

UPDATE

The curl event call to their new Web API is still an issue. We've been working with the company that owns the Web API but have yet to be able to figure it out. Again, this worked until they went into a new environment and changed the URL. I don't know what else we can do on our side in our Azure App Service.

If I call another public Web API call from the Console, then it work fine.

Here are 2 calls that work fine from the console:

curl -v https://api.apis.guru/v2/list.json
curl -v https://jsonplaceholder.typicode.com/users

Any other thoughts?

0

There are 0 answers