简体   繁体   中英

googles recaptcha(version 2) response

How do you handle the JSON response from google?

This is how I get the JSON:

 res.on('data', function (chunk) {
            //process.stdout.write(chunk);//formats it like I need it

            var lines =JSON.parse(chunk);
});

What I get from google(eg), how the google JSON looks like:

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

What I thought would work:

JSON.parse("{ success: false, 'error-codes': [ 'missing-input-response' ] }").success;

Of course it does not work, because it is not proper formated.

What actually works(but for that I need to transform the JSON from google):

JSON.parse("{  "success": false,  "error-codes": [    "missing-input-response"  ]}").success

Then I found this :

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function(chunk) {//chunk is the JSON from google
    var lines = chunk.split("\n");
    if(lines.length >= 2) {
        if(lines[0] == 'true')
            that._recaptcha_response.is_valid = true;
        that._recaptcha_response.error = lines[1];
    }
    that.emit('data', that._recaptcha_response);
  });
});

But this does not seem to work on my place, maybe they changed the JSON from recaptcha version 1 to version 2 ?!.

UPDATED

A deeper look showed the following:

Look here

It looks like the JSON may already have been parsed for you. { success: false, 'error-codes': [ 'missing-input-response' ] } is how Node.js prints out the object.

Ok, this solved my problem:

JSON.parse(blal.replace(/\n|\r/g, "")).success;

Assuming blal is googles JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM