Agent
Agent is a client-side request module inspired by SuperAgent.
API
The request object
var agent = var request =
Agent can be used by composing methods:
requestmethod'GET'datax: y
These methods also act as getters when no parameter is supplied.
Agent can also be used by passing arguments:
var request = // defaults to POSTvar request = // defaults to POST, no datavar request =
You can also mix and match:
var request = request
Other methods:
request // set request headerrequest // get request headerrequest // get all request header request // boolrequest // duhrequestrequest // don't use "password" as your password
Encoders, decoders
Agent encodes data you send and decodes data it receives based on content-type.
By default, it encodes and decodes application/x-www-form-urlencoded
and application/json
(however an application/x-www-form-urlencoded
response is not common).
You can add a new response body decoder by doing:
agent; request
Or an encoder for data you send:
// This is already in Agent, this is an exampleagent; request// the request will be sent with JSON rather than url encoded.requestdataa: 1 b: 2
The response object
The response obejct you get back when requesting contains a few useful properties:
responsetext // response textresponsebody // parsed response text based on the content-type header fieldresponsestatus // response statusresponseheader // response header, as an object // and some added sugar, courtesy of SuperAgent responseinforesponseokresponseclientErrorresponseserverErrorresponseerrorresponseacceptedresponsenoContentresponsebadRequestresponseunauthorizedresponsenotAcceptableresponsenotFound
Test
Simply run
npm test
And fire up a browser on http://localhost:9090