I am new to node.js, and would appreciate some assistance.
I am currently writing code, that simply fetches a request id, via parsing the json, and then relays that id over to another http call, to derive a token, to then input into html via puppeteer.
*Currently, when I attempt to parse json via nodejs's json parse command, I receive the error:
UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
My intention is to derive the value of attribute 'request', so It can be referenced as a constant in the subsequent steps.
However, I receive the above error.
The JSON format looks like this when I make the http request.
{"status":1,"request":"123452590"}
(Request value in this test case would be 123452590 which is what I want to obtain and store as constant
Any help would be really appreciated.
Currently, the code is structured in the following manner:
method: 'userrecaptcha',
key: xxx
googlekey: 'xxx',
pageurl: 'http://test.ca',
json: 1
};
var request = require('request');
const response = await request.post('http://2captcha.com/in.php', {form: formData});
const requestId = JSON.parse(response).request;
console.log (requestId)
async function pollForRequestResults(key, id, retries = 30, interval = 1500, delay = 15000) {
await timeout(delay);
return poll({
taskFn: requestCaptchaResults(key, id),
interval,
retries
});
}
function requestCaptchaResults(key, requestId) {
const url = `http://2captcha.com/res.php?key=${key}&action=get&id=${requestId}&json=1`;
return async function() {
return new Promise(async function(resolve, reject){
const rawResponse = await request.get(url);
const resp = JSON.parse(rawResponse);
if (resp.status === 0) return reject(resp.request);
resolve(resp.request);
});
}
}
const timeout = millis => new Promise(resolve => setTimeout(resolve, millis))
})()```
const requestId = JSON.parse(response).request;