人偶未能发布

时间:2018-12-12 16:46:43

标签: javascript node.js puppeteer

我试图拦截一个请求,添加一些标头,将方法更改为post并传递一些postData,但是由于仍然是GET的方法似乎失败了,而且postData也未定义。

有我的代码:

await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
  var overrides;
  if (interceptedRequest.url() === 'https://www.example.com/') {
    console.dir(querystring.stringify(query));
    let headers = interceptedRequest.headers();
    headers['host'] = 'example.com/';
    headers['origin'] = 'https://www.example.com/';
    headers['referer'] = 'https://www.example.com/test';
    headers['x-requested-with'] = 'XMLHttpRequest';
    headers['accept'] = 'application/json, text/javascript, */*; q=0.01';
    headers['accept-encoding'] = 'gzip, deflate, br';
    headers['content-type'] = 'application/json; charset=UTF-8';
    overrides = {
      'method': 'POST',
      'postData': querystring.stringify(query),
      'headers': headers
    };
  }
  interceptedRequest.continue(overrides);
});

然后,如果我拦截了响应:

...
 _resourceType: 'document',
 _method: 'GET',
 _postData: undefined,
 _headers:
  { 'upgrade-insecure-requests': '1',
    'user-agent':
     'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/72.0.3582.0 Safari/537.36',
    host: 'example.com',
    origin: 'https://www.example.com/',
    referer: 'https://www.example.com/test',
    'x-requested-with': 'XMLHttpRequest',
    accept: 'application/json, text/javascript, */*; q=0.01',
    'accept-encoding': 'gzip, deflate, br',
    'content-type': 'application/json; charset=UTF-8' }
  ...

谢谢

0 个答案:

没有答案