Webform REST reCaptcha post request

Created on 21 January 2022, almost 3 years ago
Updated 17 February 2023, almost 2 years ago

Problem/Motivation

I am trying to send the reCaptcha token with the webform rest call to the webform but I got the following error.
{
"message": "Submitted Data contains validation errors.",
"error": {
"captcha_response": "The answer you entered for the CAPTCHA was not correct."
}
}

Steps to reproduce

after you add a reCAPTCHA to your webform, try sending the request.
{
"webform_id":"contact",
"name":"something",
"g-recaptcha-response":"-8Fcwcr4tdztPoRdadadvi5AYNvuGc9qKprNR3Vfrg7nKlYJr9nMgrgrgrgrgrgTQ4HXahVKyGb7nHEkPPdK5KCPSp1RZ-WM0Dkyr5vt8ghCyDjzvHVnIATTJYVHSsaHemJh2TdnazXX2orruskoTjZb6aMM9tRaGecNkqdHjnZYVDGPvhZ7s0Y6UfYivIeR_bwHxCU3zN6LKTdajM5IiARGV-WAFKD1LKzIQjzeAx6uuhxg6kIAv4RIWx7rm5gOdhGtOAAO5z7hRHp6cKWegBQQUnTilppoIA-C7tXMSZY8fTZPzDGmPV2Y_D1m9LkUpLay7jxZ1iq8hmmJaAY2x7UxLTvw"
}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

4.0

Component

User interface

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡§πŸ‡·Brazil AndersonMendes0

    I am also having the same impasse!

  • πŸ‡ΊπŸ‡ΈUnited States cdupree

    Perhaps not helpful, but how are you accessing the recaptcha response token? I can't figure that part out.

  • πŸ‡ΊπŸ‡ΈUnited States oheller

    I too was having this issue, here's the solution I arrived at:
    Requirements: Using Gatsby + Webforms + Recaptcha v3 (invisible).

    After reviewing captcha and recaptcha_v3 it seems that the verification doesn't work for a headless Drupal site. When following the xdebug path the "captcha" field never makes it into the form_state for future verification. So, it always works.

    I ended up using simple_recaptcha because they have a well defined method SimpleReCaptchaFormManager::validateCaptchaToken(). I had to add my own helper module to add my webformelement and webformhandler because simple_Recaptcha adds the form validation to the button and uses ajax in it's provided simple_recaptcha_webform handler.

    I created my own WebformElement, "HeadlessCaptcha", to add the headless_captcha element to use, and a WebformHandler "HeadlessCaptchaValidation" to handle the verification (using validateForm() instead of submitForm()).

    I've got the front end (gatsby) to generate the token and the backend to send the token via the handler to verify the token. It succeeds when submitting the webform form gatsby, and fails when trying to send the form via Postman (without the captcha token). It also fails if the correct key's aren't present.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Related to this, I was getting this error on a site:

    Symfony\Component\Serializer\Exception\NotNormalizableValueException: Could not normalize object of type "Drupal\simple_recaptcha\SimpleReCaptchaFormManager", no supporting normalizer found. in Symfony\Component\Serializer\Serializer->normalize() (line 188 of /var/www/html/vendor/symfony/serializer/Serializer.php).

    The solution was to change from reCAPTCHA v2 to v3, then it stopped becoming an issue.

Production build 0.71.5 2024