我是node.js的新手,希望能获得一些帮助。
我目前正在编写代码,该代码只是通过解析json来获取请求ID,然后将该ID中继到另一个http调用,以获取令牌,然后通过puppeteer输入html。
*当前,当我尝试通过nodejs的json parse命令解析json时,收到错误消息:
UnhandledPromiseRejectionWarning:语法错误:JSON中位置1处的意外令牌o
我的目的是派生'request'属性的值,因此在后续步骤中可以将其引用为常量。
但是,我收到上述错误。
当我发出http请求时,JSON格式如下所示。
{“状态”:1,“请求”:“ 123452590”}
(在这个测试案例中,请求值是123452590,这是我想要获取并存储为常量的值
任何帮助将不胜感激。
当前,代码的结构如下:
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;