## Methods

### _buildResponse

##### splunkjs.Http._buildResponse

Generates a unified response with the given parameters.

#### Syntax

_buildResponse: function(error, response, data)

#### 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.

#### Return

Object.

A unified response object.

#### Source (lib/http.js:237)

    _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"]);
            }
            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;
        }
    });

### del

##### splunkjs.Http.del

Performs a DELETE request.

#### Syntax

del: function(url, headers, params, timeout, callback)

#### Parameters

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. This parameter is not used.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/http.js:147)

    del: function(url, headers, params, timeout, callback) {
        var message = {
            method: "DELETE",
            headers: headers,
            timeout: timeout,
            query: params
        };
        return this.request(url, message, callback);
    },

### get

##### splunkjs.Http.get

Performs a GET request.

#### Syntax

get: function(url, headers, params, timeout, callback)

#### Parameters

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. This parameter is not used.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/http.js:102)

    get: function(url, headers, params, timeout, callback) {
        var message = {
            method: "GET",
            headers: headers,
            timeout: timeout,
            query: params
        };
        return this.request(url, message, callback);
    },

### init

##### splunkjs.Http.init

Constructor for `splunkjs.Http`.

#### Syntax

init: function()

#### Return

splunkjs.Http.

A new `splunkjs.Http` instance.

#### Source (lib/http.js:72)

    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");
    },

### makeRequest

##### splunkjs.Http.makeRequest

Encapsulates the client-specific logic for performing a request. This function is meant to be overriden by subclasses.

#### Syntax

makeRequest: function(url, message, callback)

#### Parameters

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: `(err, response)`.

#### Source (lib/http.js:211)

    makeRequest: function(url, message, callback) {
        throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
    },

### parseJson

##### splunkjs.Http.parseJson

Encapsulates the client-specific logic for parsing the JSON response.

#### Syntax

parseJson: function(json)

#### Parameters

Name | Type | Description
--- | --- | ---
json | String | The JSON response to parse.

#### Return

Object.

The parsed JSON.

#### Source (lib/http.js:223)

    parseJson: function(json) {
        throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
    },

### post

##### splunkjs.Http.post

Performs a POST request.

#### Syntax

post: function(url, headers, params, timeout, callback)

#### Parameters

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. This parameter is not used.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/http.js:124)

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

### request

##### splunkjs.Http.request

##### See: makeRequest

Performs a request.

This function sets up how to handle a response from a request, but delegates calling the request to the `makeRequest` subclass.

#### Syntax

request: function(url, message, callback)

#### Parameters

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: `(err, response)`.

#### Source (lib/http.js:171)

    request: function(url, message, callback) {
        var wrappedCallback = function(response) {
            callback = callback || function() {};
            if (response.status < 400 && response.status !== "abort") {
                callback(null, response);
            }
            else {
                callback(response);
            }
        };
        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 options = {
            method: message.method,
            headers: message.headers,
            timeout: message.timeout,
            body: body
        };
        // Now we can invoke the user-provided HTTP class,
        // passing in our "wrapped" callback
        return this.makeRequest(encodedUrl, options, wrappedCallback);
    },

## splunkjs.Http Module Globals

### encode

##### splunkjs.Http.encode

Encodes a dictionary of values into a URL-encoded format.

#### Syntax

Http.encode = function(params)

#### Parameters

Name | Type | Description
--- | --- | ---
params | Object | The parameters to URL encode.

#### Return

String.

The URL-encoded string.

#### Examples

     // should be a=1&b=2&b=3&b=4
     encode({a: 1, b: [2,3,4]})

#### Source (lib/http.js:286)

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