0

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?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
JumpIntoTheWater
  • 1,306
  • 2
  • 19
  • 46
  • You wanna use Razor from html page to call controller action method? Is it a MVC app ? – Krishna Chaitanya Jan 07 '21 at 17:37
  • no. I have a document of how to send data to service, but the example is with ajax. I need to implement it from my .net core web app. first part of `greacaptcha` is being executed in Angular.Then, I need to have a POST call. – JumpIntoTheWater Jan 07 '21 at 17:39
  • 404 is Page Not Found. Check that you are passing the correct URL to the httpclient on the request. – Russ Jan 07 '21 at 17:44
  • @Russ sorry, it's 400. I have mentioned it's `BadREquest` – JumpIntoTheWater Jan 07 '21 at 17:46
  • Hi @YanivEliav,Could you share the action code which you request by httpclient?It should be:`public IActionResult Post([FromBody]string googleToken)`.But this does not equal to your ajax,your ajax is used to post data by query,but your PostAsJsonAsync is used to post by body. – Rena Jan 08 '21 at 03:32
  • It's a subtle thing but please prefer `MediaTypeNames.Application.Json` ([1](https://learn.microsoft.com/en-us/dotnet/api/system.net.mime.mediatypenames.application)) over `"application/json"` – Peter Csala Jan 08 '21 at 08:58
  • You should not need to use `PostAsJsonAsync` you can use `SendAsync` and a properly constructed `StringContent`. Related so topic: https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request – Peter Csala Jan 08 '21 at 09:01

0 Answers0