I have to use httpClient.PostASync() method in C#.Net , I am fine with passing header information. but I am getting trouble while trying to pass the "Body" information, under a specific condition. this is working fine in Postman and Talend API Tester tools.

in postman tool : in body tab --> under "x-www-form-urlencoded" sub tab --> add the following value .. grant_type : client_credentials

in Talend Tool : in body section --> select "Form" option from right side dropdown (not text / file options) . --> check the "application/x-www-form-urlencoded" checkbox. --> add form parameter --> grant_type [Text] = client_credentials

here is my code block ..

        HttpClient httpClientToken = new HttpClient();
        var values = new Dictionary<string, string> { { "grant_type", "client_credentials" } };
        var httpContent = new FormUrlEncodedContent(values);
        httpClientToken.DefaultRequestHeaders.Add("Authorization", "Basic aFFqTm53eUdFRTV6M2pwY0Vjc3dUU0RNSWJRTUxHMGw6QzRzcmZGRHlkR3F5REc3cw==");
        httpClientToken.DefaultRequestHeaders.Add("grant_type", "client_credentials");
        httpClientToken.Timeout = TimeSpan.FromMinutes(60);
        string token = "";
        var response = httpClientToken.PostAsync(appConfiguration.TokenAPIEndpoint, httpContent);
        string responseBody = response.Result.Content.ReadAsStringAsync().Result;

I am getting error as missing credentials for the above code.

Thanks for the help in advance.

0

There are 0 best solutions below