Client

Server.Interfaces.HTTP. Client

# new Client()

This is the HTTP Client class. It does not need to be instantiated, so you can safely ignore the constructor. You can access methods on it in this way - "interface.client.method". This class contains methods to make outgoing HTTP requests and receive responses from these requests.

Example
const httpClient = req.core.module('http', 'interface').client;

Methods

# (static) delete(url, data, options, cb)

This method allows you to make an HTTP/S request to an endpoint, with the verb of the request set to DELETE. Such requests are commonly used to instruct a server to delete an object from the server. You are also able to attach a request body and/or headers to the request.

Parameters:
Name Type Description
url string

Request URL

data object

Request Body Data

options object

Options Object

cb function

Callback Function

Example
const httpClient = req.core.module('http', 'interface').client;
httpClient.delete('https://www.restfulnights.com/api/v1/users/101', null, {}, function(err, res) {
    if(res.statusCode == 200) {
        console.log('User Deleted Successfully');
    }
});

# (static) get(url, cb)

This method allows you to make an HTTP/S request to an endpoint, with the verb of the request set to GET. Such requests are commonly used to instruct a server to fetch one or more objects from the server. You are also able to attach a request body and/or headers to the request.

Parameters:
Name Type Description
url string

Request URL

cb function

Callback Function

Example
const httpClient = req.core.module('http', 'interface').client;
httpClient.get('https://www.restfulnights.com/api/v1/users', function(err, res) {
    if(res.statusCode == 200) {
        console.log('User List Retrieved Successfully');
    }
});

# (static) post(url, data, options, cb)

This method allows you to make an HTTP/S request to an endpoint, with the verb of the request set to POST. Such requests are commonly used to instruct a server to create a new object on the server. You are also able to attach a request body and/or headers to the request.

Parameters:
Name Type Description
url string

Request URL

data object

Request Body Data

options object

Options Object

cb function

Callback Function

Example
const httpClient = req.core.module('http', 'interface').client;
httpClient.post('https://www.restfulnights.com/api/v1/users', null, {email: '[email protected]'}, function(err, res) {
    if(res.statusCode == 201) {
        console.log('User Created Successfully');
    }
});

# (static) put(url, data, options, cb)

This method allows you to make an HTTP/S request to an endpoint, with the verb of the request set to PUT. Such requests are commonly used to instruct a server to update one or more properties on an object on the server. You are also able to attach a request body and/or headers to the request.

Parameters:
Name Type Description
url string

Request URL

data object

Request Body Data

options object

Options Object

cb function

Callback Function

Example
const httpClient = req.core.module('http', 'interface').client;
httpClient.put('https://www.restfulnights.com/api/v1/users/101', {country: 'Australia'}, {}, function(err, res) {
    if(res.statusCode == 200) {
        console.log('User Updated Successfully');
    }
});

# (static) request(req, cb)

This method allows you to make an HTTP/S request to an endpoint, setting the verb to whatever you would like, in addition to providing the URL to send the request to, an object containing headers to accompany the request, a request body and setting the encoding type of the request. All of this information describing the request that you wish to send is passed within a Request Object to this method.

Parameters:
Name Type Description
req object

Request Object

cb function

Callback Function

Example
req.core.module('http', 'interface').client.request({
  "url": "https://www.google.com:8080/path1/path2?test=now",
  "headers": {
     "Content-Type": "application/json",
     "Content-Length": data.length
  },
  "method": "POST",
  "data": {"test": "value"},
  "encoding": "utf8"
}, function(err, res) {
   console.log(res);
});