Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Recaptcha not working with axios

I am encountering an error while trying to verify my recaptcha witch axios

try{
            let result = await axios.post(
                'https://www.google.com/recaptcha/api/siteverify',
                {
                    secret: '6LcarRkTAAAAAPDNrruugUg1fJqOJnH-uvVa5KIL',
                    response: response
                },
                {
                    headers: {
                        "Content-Type": "application/json"
                    }
                });

            let data = result.data || {};
            if(!data.success){
                throw({
                    success: false,
                    error: 'response not valid'
                })
            }
        }catch(err){
            console.log(err);
            throw err.response ? err.response.data : {success: false, error: 'verifyCatpcha Error'}
        }

I am always getting the error

{ success: false,
'error-codes': [ 'missing-input-response', 'missing-input-secret' ] }

I tried it with postman and it works fine. Something wrong with my header?

like image 747
Silve2611 Avatar asked Jan 03 '18 16:01

Silve2611


3 Answers

You need to add one more key to your request: secret. The error message is caused by missing response and secret parameters when sending POST request.

UPDATE: The POST params in the doc are not JSON, they must be passed as query string. That's why the error says it's missing both missing-input-response and missing-input-secret

axios.post(
  `https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${response}`,
  {},
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
    },
  },
);

Reference: Doc

like image 141
Ryan Wu Avatar answered Oct 19 '22 18:10

Ryan Wu


I managed to solve it be adding params. I will accept Ryan Wus answer cause it is basically the same with different writing.

    try{
        let result = await axios({
            method: 'post',
            url: 'https://www.google.com/recaptcha/api/siteverify',
            params: {
                secret: '6LcarRkTAAAAAPDNrruugUg1fJqOJnH-uvVa5KIL',
                response: response
            }
        });
        let data = result.data || {};
        if(!data.success){
            throw({
                success: false,
                error: 'response not valid'
            })
        }
    }catch(err){
        console.log(err);
        throw err.response ? err.response.data : {success: false, error: 'captcha_error'}
    }
like image 11
Silve2611 Avatar answered Oct 19 '22 17:10

Silve2611


https://stackoverflow.com/a/48083886/5426839

The POST params in the doc are not JSON, they must be passed as query string. That's why the error says it's missing both missing-input-response and missing-input-secret

Not opposite to the highest answer, just shorter syntax:

axios.post('https://www.google.com/recaptcha/api/siteverify', undefined, {
        params: {
            secret: RECAPTCHA_V3_SECRET,
            response: recaptchaToken
        }
    })
like image 6
Ruce Bee Avatar answered Oct 19 '22 17:10

Ruce Bee