Generates a unified response with the given parameters.
Generates a unified response with the given parameters.
Name | Type | Description |
---|---|---|
error | Object | An error object, if one exists for the request. |
response | Object | The response object. |
data | Object | The response data. |
A unified response object.
_buildResponse: function(error, response, data) {
var complete_response, json = {};
var contentType = null;
if (response && response.headers) {
contentType = utils.trim(response.headers["content-type"] || response.headers["Content-Type"] || response.headers["Content-type"] || response.headers["contentType"]);
}
if (utils.startsWith(contentType, "application/json") && data) {
try {
json = this.parseJson(data) || {};
}
catch(e) {
logger.error("Error in parsing JSON:", data, e);
json = data;
}
}
else {
json = data;
}
if (json) {
logger.printMessages(json.messages);
}
complete_response = {
response: response,
status: (response ? response.statusCode : 0),
data: json,
error: error
};
return complete_response;
}
});
Performs a DELETE request.
Name | Type | Description |
---|---|---|
url | String | The URL of the DELETE request. |
headers | Object | An object of headers for this request. |
params | Object | Query parameters for this request. |
timeout | Number | A timeout period. |
callback | Function | The function to call when the request is complete: |
del: function(url, headers, params, timeout, callback) {
var message = {
method: "DELETE",
headers: headers,
timeout: timeout,
query: params
};
return this.request(url, message, callback);
},
Performs a GET request.
Name | Type | Description |
---|---|---|
url | String | The URL of the GET request. |
headers | Object | An object of headers for this request. |
params | Object | Parameters for this request. |
timeout | Number | A timeout period. |
callback | Function | The function to call when the request is complete: |
get: function(url, headers, params, timeout, callback, isAsync) {
var message = {
method: "GET",
headers: headers,
timeout: timeout,
query: params
};
return this.request(url, message, callback, isAsync);
},
Constructor for splunkjs.Http
.
A new splunkjs.Http
instance.
init: function() {
// We perform the bindings so that every function works
// properly when it is passed as a callback.
this.get = utils.bind(this, this.get);
this.del = utils.bind(this, this.del);
this.post = utils.bind(this, this.post);
this.request = utils.bind(this, this.request);
this._buildResponse = utils.bind(this, this._buildResponse);
// Set our default version to "none"
this._setSplunkVersion("none");
// Cookie store for cookie based authentication.
this._cookieStore = {};
},
Encapsulates the client-specific logic for performing a request. This function is meant to be overriden by subclasses.
Name | Type | Description |
---|---|---|
url | String | The encoded URL of the request. |
message | Object | An object with values for method, headers, timeout, and encoded body. |
callback | Function | The function to call when the request is complete: |
makeRequest: function(url, message, callback) {
throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
},
Encapsulates the client-specific logic for parsing the JSON response.
Name | Type | Description |
---|---|---|
json | String | The JSON response to parse. |
The parsed JSON.
parseJson: function(json) {
throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
},
Performs a POST request.
Name | Type | Description |
---|---|---|
url | String | The URL of the POST request. |
headers | Object | An object of headers for this request. |
params | Object | Parameters for this request. |
timeout | Number | A timeout period. |
callback | Function | The function to call when the request is complete: |
post: function(url, headers, params, timeout, callback) {
headers["Content-Type"] = "application/x-www-form-urlencoded";
var message = {
method: "POST",
headers: headers,
timeout: timeout,
post: params
};
return this.request(url, message, callback);
},
Performs a request.
This function sets up how to handle a response from a request, but delegates calling the request to the makeRequest
subclass.
Name | Type | Description |
---|---|---|
url | String | The encoded URL of the request. |
message | Object | An object with values for method, headers, timeout, and encoded body. |
callback | Function | The function to call when the request is complete: |
request: function(url, message, callback, isAsync) {
var that = this;
var query = utils.getWithVersion(this.version, queryBuilderMap)(message);
var post = message.post || {};
var encodedUrl = url + "?" + Http.encode(query);
var body = message.body ? message.body : Http.encode(post);
var cookieString = that._getCookieString();
if (cookieString.length !== 0) {
message.headers["Cookie"] = cookieString;
// Remove Authorization header
// Splunk will use Authorization header and ignore Cookies if Authorization header is sent
delete message.headers["Authorization"];
}
var options = {
method: message.method,
headers: message.headers,
timeout: message.timeout,
query: message.query,
body: body
};
// Now we can invoke the user-provided HTTP class,
// passing in our "wrapped" callback
if(isAsync) {
return this.makeRequestAsync(encodedUrl, options);
}
else {
var wrappedCallback = function(response) {
callback = callback || function() {};
// Handle cookies if 'set-cookie' header is in the response
var cookieHeaders = response.response.headers['set-cookie'];
if (cookieHeaders) {
utils.forEach(cookieHeaders, function (cookieHeader) {
var cookie = that._parseCookieHeader(cookieHeader);
that._cookieStore[cookie.key] = cookie.value;
});
}
// Handle callback
if (response.status < 400 && response.status !== "abort") {
callback(null, response);
}
else {
callback(response);
}
};
return this.makeRequest(encodedUrl, options, wrappedCallback);
}
},
Encodes a dictionary of values into a URL-encoded format.
Name | Type | Description |
---|---|---|
params | Object | The parameters to URL encode. |
The URL-encoded string.
// should be a=1&b=2&b=3&b=4
encode({a: 1, b: [2,3,4]})
Http.encode = function(params) {
var encodedStr = "";
// We loop over all the keys so we encode them.
for (var key in params) {
if (params.hasOwnProperty(key)) {
// Only append the ampersand if we already have
// something encoded, and the last character isn't
// already an ampersand
if (encodedStr && encodedStr[encodedStr.length - 1] !== "&") {
encodedStr = encodedStr + "&";
}
// Get the value
var value = params[key];
// If it's an array, we loop over each value
// and encode it in the form &key=value[i]
if (value instanceof Array) {
for (var i = 0; i < value.length; i++) {
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[i]) + "&";
}
}
else if (typeof value === "object") {
for(var innerKey in value) {
if (value.hasOwnProperty(innerKey)) {
var innerValue = value[innerKey];
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[innerKey]) + "&";
}
}
}
else {
// If it's not an array, we just encode it
encodedStr = encodedStr + key + "=" + encodeURIComponent(value);
}
}
}
if (encodedStr[encodedStr.length - 1] === '&') {
encodedStr = encodedStr.substr(0, encodedStr.length - 1);
}
return encodedStr;
};
})();