I have the following POST call in ajax
and I would like to convert and implement the same call in .net Core 3.1
.
this is the full code of which the ajax code is part of.
grecaptcha.ready(function() {
grecaptcha.execute('ABCDEF', {
action: 'homepage'
}).then(function(token)
) {
$.ajax({
type: "POST",
url: pathName + "/Login/SetToken/?captchaToken=" + token,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response == false) {
document.getElementById("myCapcha").removeAttribute("class");
}
}
});
});
});
So I have tried implementing it, but always getting 400(BadRequest). That's what I have tried.
private static HttpClientHandler _clientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
private async Task<bool> ValidateCaptcha(string googleToken)
{
var result = new ResultOfOperation<object>();
try
{
var response = new HttpResponseMessage();
var client = new HttpClient(_clientHandler);
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var captchaResponse = await client.PostAsJsonAsync(_googleCaptchaValidationUrl, googleToken);
if (captchaResponse != null && captchaResponse.IsSuccessStatusCode)
{
var content = await captchaResponse.Content.ReadFromJsonAsync<bool>();
result.IsSuccess = content;
}
}
catch (Exception ex)
{
}
return result.IsSuccess;
}
Does my code look ok comparing to the ajax
call?