How to get the response after a POST request in Ca

2019-04-11 00:18发布

I have this very simple code to read the response from a server endpoint after a post request. Actually I'm saving a data to a database and wait for a response before going to next step

casper.open('http://example.com/ajax.php, {
    method: 'POST',
    data: {
        'title': '<title>',
        'unique_id': '<unique_id>'
    }
});

on ajax.php file I'm trying to echo the POST request in a simple way. this will let me know easily if I'm getting the right response from the server.

echo json_encode($_POST);

I tried these snippets but I'm unable to get the response.

casper.on('page.resource.received', function(resp){
    this.echo(JSON.stringify(resp, null, 4));
});

casper.on('http.status.200', function(resp){
    this.echo(JSON.stringify(resp, null, 4));
});

casper.on('resource.received', function(resp) {
    this.echo(JSON.stringify(resp, null, 4));
});

3条回答
Fickle 薄情
2楼-- · 2019-04-11 01:14

As Roberto points out. You can use this.page.content to show the response. But you need to add the function(response) in your script. For example:

casper.open('http://example.com/ajax.php', {
    method: 'POST',
    data: {
        'title': '<title>',
        'unique_id': '<unique_id>'
    }
}, function(response){
    if(response.status == 200){
        require('utils').dump(this.page.content);
    }
});
查看更多
狗以群分
3楼-- · 2019-04-11 01:15

If you want to unit test a REST API, CasperJS is not necessarily the right tool. CasperJS allows to observe a web browser which is running a web page. So a more typical approach would be to use CasperJS to load a page that would call your REST API and you would assert the page behavior is correct (assuming the page would make something observable according the AJAX call response).

查看更多
叛逆
4楼-- · 2019-04-11 01:18

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);

or

this.echo(this.page.plainText);

in your function.

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();
查看更多
登录 后发表回答