是否可以在casper.base64encode()中传递自定义请求标头?

时间:2015-10-17 00:05:54

标签: javascript http-headers phantomjs casperjs

casper.base64encode的签名如下:

base64encode(String url [, String method, Object data])

...文档提供了如何使用data参数传递表单值的示例。但是可以传递标题字符串吗?就我而言,我需要传递Authentication可能Accept个字符串。

1 个答案:

答案 0 :(得分:0)

不,您无法使用base64encode(),因为它不会以任何方式支持标头。基础__utils__.sendAJAX()仅支持"内容类型"请求标头。你可以实现自己的。通过从clientutils module复制必要的函数并调整它们,这是朝着正确方向迈出的一步:

casper.base64encodeWithHeaders = function(url, method, data, headers) {
    return this.evaluate(function(url, method, data, headers){
        function getBinaryWithHeaders(url, method, data, headers) {
            try {
                return sendAjaxWithHeaders(url, method, data, false, {
                    overrideMimeType: "text/plain; charset=x-user-defined",
                    headers: headers
                });
            } catch (e) {
                if (e.name === "NETWORK_ERR" && e.code === 101) {
                    __utils__.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning");
                }
                __utils__.log("getBinary(): Error while fetching " + url + ": " + e, "error");
                return "";
            }
        };
        function sendAjaxWithHeaders(url, method, data, async, settings) {
            var xhr = new XMLHttpRequest(),
                dataString = "",
                dataList = [];
            method = method && method.toUpperCase() || "GET";
            var contentType = settings && settings.contentType || "application/x-www-form-urlencoded";
            xhr.open(method, url, !!async);
            __utils__.log("sendAJAX(): Using HTTP method: '" + method + "'", "debug");
            if (settings && settings.overrideMimeType) {
                xhr.overrideMimeType(settings.overrideMimeType);
            }
            if (method === "POST") {
                if (typeof data === "object") {
                    for (var k in data) {
                        dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString()));
                    }
                    dataString = dataList.join('&');
                    __utils__.log("sendAJAX(): Using request data: '" + dataString + "'", "debug");
                } else if (typeof data === "string") {
                    dataString = data;
                }
                xhr.setRequestHeader("Content-Type", contentType);
            }
            if (settings && settings.headers) {
                for(var header in settings.headers) {
                    if (settings.headers.hasOwnProperty(header)) {
                        xhr.setRequestHeader(header, settings.headers[header]);
                    }
                }
            }
            xhr.send(method === "POST" ? dataString : null);
            return xhr.responseText;
        }

        return __utils__.encode(getBinaryWithHeaders(url, method, data, headers))
    }, url, method, data, headers);
}

你可以像这样使用它:

casper.then(function(){
    var response = this.base64encodeWithHeaders("http://example.com", "GET", null, {
        "Accept": "text/plain",
        "Authentication": "let me in!"
    });
    this.echo(response);
});