So I have some code from a previous working project. but for some reason when I use the relevant code in my new project it refuses to work. Essentially I am trying to make a call out to an api and receive their token. I have to pass in the body of the request a grant_type and then my username and password. Now everything works fine up until I hit the line client.PostAsync();
Then the debugger just kinda stops?(no exceptions or errors). I mean it continues to run but it never hits the next line.
I have tried .ConfigureAwait(false)
still nothing. I have tried await client.PostAsync()
. I've tried just about everything I can find here on StackOverflow. I am not sure what I'm missing but then again thats why I'm making this post. Maybe theres something different between the projects but I checked the references and they all seem to match.
Additional context: The code that is executing is a restful api (not a console application). So its an api reaching out to another api to receive a token and then either make more calls with the aforementioned token and/or then do some business logic with.
Edit: Just remembered something. In post man I can make the call correctly and I get the result so the destination is working.
here is the relevant code:
private HttpClient HttpClientSetup()
{
var client = new HttpClient(new LoggingHandler(new HttpClientHandler())) { BaseAddress = new Uri(_apiBaseUrl), Timeout = TimeSpan.FromMilliseconds(45000) };
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
private myObj GetToken()
{
HttpClient client = HttpClientSetup();
var form = new Dictionary<string, string>
{
{"grant_type", "client_credentials"},
{"client_id", _username},
{"client_secret", _password},
};
try
{
HttpResponseMessage response = client.PostAsync("auth/token", new FormUrlEncodedContent(form)).Result;
//more stuff
}
catch(Exception e)
{
//stuff
}
}