How to download a file through a custom POST request with CasperJS

元气小坏坏 提交于 2019-12-02 06:56:22

casper.download() happily accepts a serialized form instead of an object, so you can still use it. You just have to serialize the form in the page context beforehand:

var formData = casper.evaluate(function(){
  return $('form#theirForm').serialize();
});

var url;
casper.download(url, targetFile, 'POST', params);

The only problem might be, that another mimeType is used: "text/plain; charset=x-user-defined".

In that case, you will have to recreate the whole cascade of functions that go into casper.download():

var url;
var response = casper.evaluate(function(url){
    var params = $('form#theirForm').serialize();
    var data = __utils__.sendAJAX(url, 'POST', params, false);
    return __utils__.encode(data);
}, url);

var cu = require('clientutils');

fs.write("test.zip", cu.decode(response), 'wb');

"application/x-www-form-urlencoded" is used by default for __utils__.sendAJAX().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!