Commit 87f4d0bd 87f4d0bd744e08e749744a4e8bc7355ceb454ff7 by Nicolas Perriault

refactored PR #30 - Add request method and request data to the base64encode() method

1 parent 9ad70d39
......@@ -125,9 +125,9 @@
* @param String data The data to send, optional
* @return string Base64 encoded result
*/
base64encode: function(url,method,data) {
return this.evaluate(function(url,method,data) {
return __utils__.getBase64(url,method,data);
base64encode: function(url, method, data) {
return this.evaluate(function(url, method, data) {
return __utils__.getBase64(url, method, data);
}, { url: url, method: method, data: data });
},
......
......@@ -224,12 +224,12 @@
* contents.
*
* @param String url The resource url
* @param String method The request method, optional
* @param String method The request method, optional (default: GET)
* @param Object data The request data, optional
* @return String Base64 contents string
*/
this.getBase64 = function(url,method,data) {
return this.encode(this.getBinary(url,method,data));
this.getBase64 = function(url, method, data) {
return this.encode(this.getBinary(url, method, data));
};
/**
......@@ -243,38 +243,33 @@
*/
this.getBinary = function(url, method, data) {
try {
var xhr = new XMLHttpRequest();
if (method === undefined || ["GET","get","POST","post"].indexOf(method) == -1) {
var xhr = new XMLHttpRequest(), dataString = "";
if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) {
method = "GET";
} else {
method = method.toUpperCase();
}
xhr.open(method, url, false);
this.log("using HTTP method: '" + method + "'", "debug");
this.log("getBinary(): Using HTTP method: '" + method + "'", "debug");
xhr.overrideMimeType("text/plain; charset=x-user-defined");
if (method == "POST") {
if(data === undefined) {
data_str = "";
} else {
data_str = "";
for (k in data) {
if (typeof(k) == "string" && typeof(data[k]) == "string") {
data_str += "&" + escape(k) + "=" + escape(data[k]);
}
if (method === "POST") {
if (typeof data === "object") {
var dataList = [];
for (var k in data) {
dataList.push(escape(k) + "=" + escape(data[k].toString()));
}
data_str = data_str.substring(1);
dataString = dataList.join('&');
this.log("getBinary(): Using request data: '" + dataString + "'", "debug");
}
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.log("using request data: '" + data_str + "'", "debug");
xhr.send(method == "POST" ? data_str : null);
xhr.send(method === "POST" ? dataString : null);
return xhr.responseText;
} catch (e) {
if (e.name === "NETWORK_ERR" && e.code === 101) {
this.log("unfortunately, casperjs cannot make cross domain ajax requests", "warning");
this.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning");
}
this.log("error while fetching " + url + ": " + e, "error");
this.log("getBinary(): Error while fetching " + url + ": " + e, "error");
return "";
}
};
......