且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CasperJS中进行POST请求后获取响应

更新时间:2023-12-03 16:00:58

我也遇到了同样的问题,将查询发布到ElasticSearch,但我无法检索结果.

I've been facing the same problem POSTing a query to ElasticSearch and I could not retrieve the results.

据我了解,如果要检索脚本回显的数据,解决方案可能是这样:

As far as I can understand if you want to retrieve the data echoed by your script the solution could be this:

this.echo(this.page.content);

this.echo(this.page.plainText);

在您的功能中.

例如(我的ElasticSearch案例):

For example (my case with ElasticSearch):

/*
 * SOME VAR DEFINITIONS HERE    
 */

casper.start();

casper.then( function() {
    // the next var is very specific to ElasticSearch
    var elasticQuery = JSON.stringify (
      {
        'size' : 20,
        'query' : {
          'filtered' : {
            'filter' : { 'term' : { 'locked' : false } }
          }
        },
        'sort': { 'lastScrapeTime': { 'order': 'asc' } }
      }
    );

    var elasticRequest = {
      method: 'POST',
      data: elasticQuery
    }

    this.thenOpen( <<YOUR URL>>, elasticRequest, function (response) {
      // dump response header
      require('utils').dump(response);

      // echo response body
      this.echo(this.page.content);

      // echo response body with no tags added (useful for JSON)
      this.echo(this.page.plainText);
    }); 
  }
);

casper.run();