Silve2611
Silve2611

Reputation: 2278

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?

Upvotes: 14

Views: 15454

Answers (4)

ziv
ziv

Reputation: 3870

The posted data needs to be in form and not JSON. The easiest way is to use axios postForm method:

const result=await axios.postForm("https://www.google.com/recaptcha/api/siteverify", {secret:"...", response:"..."});

Upvotes: 1

Ruce Bee
Ruce Bee

Reputation: 101

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
        }
    })

Upvotes: 6

Ryan W
Ryan W

Reputation: 6173

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

Upvotes: 37

Silve2611
Silve2611

Reputation: 2278

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'}
    }

Upvotes: 12

Related Questions