chain-request
v0.0.7
Published
A simple chainable, readable object with the intense to make speaking HTTP sane.
Downloads
26
Maintainers
Readme
chain-request
A simple chainable, readable object with the intense to make speaking HTTP sane.
Installation
> npm install --save chain-request
Usage
Just include the module and name it, e.g.:
var request = require('chain-request');
Examples
Use the google REST-API
new request()
.get('http://ajax.googleapis.com/ajax/services/search/web')
.expects(request.CONTENT_TYPE.JSON)
.data({
v: '1.0',
q: 'test'
})
.send(function(data) {
data.responseData.results.forEach(function(result) {
console.log(result);
});
});
Upload a file with authorization Header
new request()
.post('https://build.phonegap.com/api/v1/apps')
.expects(request.CONTENT_TYPE.JSON)
.addAuthorizationHeader('Basic: a2Vrc2U6a3VjaGVu')
.data('app.zip')
.sendMultipart()
.send(function(result) {
console.log(result);
})
Get a specific jira task
new request()
.get('https://my-jira.tld/rest/api/2/issue/FOO-39')
.expects(request.CONTENT_TYPE.JSON)
.addAuthorizationHeader('Basic: a2Vrc2U6a3VjaGVu')
.send(function(result) {
console.log(result);
})
API
Functions
Kind: global function
| Param | Type | Default | Description | | --- | --- | --- | --- | | [debug] | boolean | false | enable debugging with true |
get() ⇒ object
make a get request
Kind: global function
Returns: object - self
put() ⇒ object
make a put request
Kind: global function
Returns: object - self
post() ⇒ object
make a post request
Kind: global function
Returns: object - self
patch() ⇒ object
make a patch request
delete() ⇒ object
make a delete request
Kind: global function
Returns: object - self
options() ⇒ object
make a options request
Kind: global function
Returns: object - self
head() ⇒ object
make a head request
Kind: global function
Returns: object - self
Kind: global function
Returns: object - self
data(data) ⇒ object
set the data for the request
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | data | object | data as json object |
addHeader(key, value) ⇒ object
add a header
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | key | string | header key | | value | string | value of the header entry |
allowUnauthorized() ⇒ object
Don't verifie against the list of supplied CAs.
Kind: global function
Returns: object - self
addAuthorizationHeader(authStr) ⇒ object
set a authorization header
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | authStr | string | authirization string |
setSendContentType(contentType) ⇒ object
set which type of format you want to send, default ist json
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | contentType | object | a valid content type |
sendsPlain() ⇒ object
function to send plain text to the server only make sense with post or put
Kind: global function
Returns: object - self
sendMultipart() ⇒ object
defines a multipart request for files, used with post() and data(filename)
Kind: global function
Returns: object - self
setBufferSize(bufferSize) ⇒ object
set the buffer size for a multipart request default is 4 * 1024
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | bufferSize | number | size in byte |
setEncoding(encoding) ⇒ object
set the response encoding, default is utf-8
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | encoding | string | a valid encoding |
expects(type) ⇒ object
set the format for the send callback, default is json
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | type | string | a valid content type |
send([callback]) ⇒ object
perform the previously created request
Kind: global function
Returns: object - self
| Param | Type | Description | | --- | --- | --- | | [callback] | function | function called when the request is ready |