I am trying to implement a Web Service call in my Asp.Net Core project using HttpClient and HttpResponseMessage. I have to post a json data to an api and in return api will also send the reponse in json. I have tried the below code but its giving me "statuscode: 411, reasonphrase: 'length required' in authorize.net"
I have also tried explicitly adding Content-Length property of header but also i am getting the same response.
Any help on this will be highly appreciated, as i am stuck from past one day.
public async Task<IActionResult> CallAuthorizeNetPG()
{
using (var client = new HttpClient())
{
var postedData = @"{
""authenticateTestRequest"": {
""merchantAuthentication"": {
""name"": ""566Mw6rrjNF"",
""transactionKey"": ""65rs7HeJVnu4n744""
}
}
}";
client.BaseAddress = new Uri("https://apitest.authorize.net/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent content = new StringContent(JsonConvert.SerializeObject(postedData), Encoding.UTF8, "application/json");
content.Headers.Remove("Content-Length");
content.Headers.Add("Content-Length", postedData.Length.ToString());
try
{
HttpResponseMessage response = await client.PostAsJsonAsync("xml/v1/request.api", content);
if (response.IsSuccessStatusCode)
{
RootObject department = await response.Content.ReadAsAsync<RootObject>();
}
}
catch (Exception ex)
{
}
}
return View();
}