# http-api-client [![Build Status](https://travis-ci.org/conde-nast-international/http-api-client.png?branch=master)](https://travis-ci.org/conde-nast-international/http-api-client)

An easy to use NodeJS HTTP API client library.

# Installation

```bash
npm install http-api-client --save
```

# Usage

```js
var client = require('http-api-client');

client.request({
    url: 'http://hawttrends.appspot.com/api/terms/'
}).then(function (response) {
    response.getStatusCode(); // returns the HTTP status code
    console.log(response.getData()); // returns the string representation of the response body
    response.getBuffer(); // returns the raw response Buffer object http://nodejs.org/api/buffer.html
    response.getNativeResonse(); // returns the native NodeJS repsonse object
    response.getJSON(); // returns a JSON-parsed repsonse
});
```

# API

```js
client.request({
    url: 'http://example.com', // required, protocol can be HTTPS
    method: 'POST',  // optional
    encoding: 'utf8', // optional
    parameters: { param: 'value' } // optional POST parameters,
    cookies: { // optional object containing cookies
        cookieName: cookieValue
    },
    data: rawRequestBody // optional raw request body data
    files: { // optional file uploads
        fieldname: {
            name: 'filename',
            type: 'application/octet-stream',
            data:  fileDataBuffer
        }
    }
}).then(function (/* Response object */response) {
    // Success
    response.request({
        url: 'http://example.com/something'
    }); // will make a request perserving the cookies from the previous request
}, function (error) {
    // Failure
});
```

