Home > Mobile >  Google Recaptcha returning string instead of json
Google Recaptcha returning string instead of json

Time:11-27

I am having trouble with google recaptcha. The point is that frontend is giving me the token and when I check in the backend if that token is okey the api of google recaptcha is giving me the data by a string (when it should return a json with the status of the token.

I am using nodejs.

This is the code for validating the captcha in backend and after a screenshot of the value of "scoreRecaptcha

async function validateCaptcha(req) {
  const recaptcha = process.env.CAPTCHA_PRIVATE;
  const token = req.body.token;
  const scoreRecaptcha = await axios.post(
    `https://www.google.com/recaptcha/api/siteverify?secret=${recaptcha}&response=${token}`
  );
  return scoreRecaptcha.data.success;
}

Screenshoot value returned by recaptcha api

I except that scoreRecaptcha.data.success gives me a value of true or false for knowing if captcha is okey

CodePudding user response:

Try this code

  const scoreRecaptcha = await axios.post(
    `https://www.google.com/recaptcha/api/siteverify?secret=${recaptcha}&response=${token}`,
    '',
    {
        headers: {
            'Accept-Encoding': 'application/json',
        }
    }
  );
  • Related