I have the below code to make an HTTP request to an external endpoint, which throws me a 422 status code which is Unprocessable Entity. The same request payload works fine when directly invoked the external URL using Postman.
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var Json = JsonConvert.SerializeObject(loanRequest, new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
IgnoreSerializableAttribute = false
}
});
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "externalEndPointURL");
request.Content = new StringContent(Json,Encoding.UTF8,"application/json");//CONTENT-TYPE header
HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("content-type", "application/json; charset=utf-8");
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
await httpClient.SendAsync(request).ContinueWith(task => {
Console.WriteLine($"Response {task.Result}");
});
}
//Request Pay load
{
"Merchant": "GAP",
"Lender": "BEN",
"RateType": "VAR",
"RepaymentType": "PI",
"PropertyUsage": "INV",
"CustomerRate": 0.0429,
"LoanTerm": 20,
"BorrowingAmount": 600000,
"RateTerm": null
}
EDIT
Below is the comparison of the two request headers.