Giter VIP home page Giter VIP logo

wreck's Introduction

wreck Logo

HTTP Client Utilities

Build Status

Lead Maintainer: Wyatt Preul

Usage

Basic

var Wreck = require('wreck');

Wreck.get('https://google.com/', function (err, res, payload) {
    /* do stuff */
});

Advanced

var Wreck = require('wreck');

var method = 'GET'; // GET, POST, PUT, DELETE
var uri    = 'https://google.com/';
var readableStream = Wreck.toReadableStream('foo=bar');

// all attributes are optional
var options = {
    payload:   readableStream || 'foo=bar' || new Buffer('foo=bar'),
    headers:   { /* http headers */ },
    redirects: 3,
    timeout:   1000,    // 1 second, default: unlimited
    maxBytes:  1048576, // 1 MB, default: unlimited
    rejectUnauthorized: true || false,
    downstreamRes: null,
    agent: null,         // Node Core http.Agent
    secureProtocol: 'SSLv3_method' // The SSL method to use
};

var optionalCallback = function (err, res) {

    /* handle err if it exists, in which case res will be undefined */

    // buffer the response stream
    Wreck.read(res, null, function (err, body) {
        /* do stuff */
    });
};

var req = Wreck.request(method, uri, options, optionalCallback);

request(method, uri, [options, [callback]])

Initiate an HTTP request.

  • method - A string specifying the HTTP request method, defaulting to 'GET'.
  • uri - The URI of the requested resource.
  • options - An optional configuration object. To omit this argument but still use a callback, pass null in this position. The options object supports the following optional keys:
    • payload - The request body as string, Buffer, or Readable Stream.
    • headers - An object containing request headers.
    • rejectUnauthorized - TLS flag indicating whether the client should reject a response from a server with invalid certificates. This cannot be set at the same time as the agent option is set.
    • redirects - The maximum number of redirects to follow.
    • agent - Node Core http.Agent. Defaults to either wreck.agents.http or wreck.agents.https. Setting to false disables agent pooling.
    • timeout - The number of milliseconds to wait without receiving a response before aborting the request. Defaults to unlimited.
    • secureProtocol - TLS flag indicating the SSL method to use, e.g. SSLv3_method to force SSL version 3. The possible values depend on your installation of OpenSSL. Read the official OpenSSL docs for possible SSL_METHODS.
  • callback - The optional callback function using the signature function (err, response) where:
    • err - Any error that may have occurred during the handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.

Returns an instance of the node.js ClientRequest object.

read(response, options, callback)

  • response - An HTTP Incoming Message object.
  • options - null or a configuration object with the following optional keys:
    • timeout - The number of milliseconds to wait while reading data before aborting handling of the response. Defaults to unlimited.
    • json - A value indicating how to try to parse the payload as JSON. Defaults to undefined meaning no parse logic.
      • true, 'smart' - only try JSON.parse if the response indicates a JSON content-type.
      • force - try JSON.parse regardless of the content-type header.
    • maxBytes - The maximum allowed response payload size. Defaults to unlimited.
  • callback - The callback function using the signature function (err, payload) where:
    • err - Any error that may have occurred while reading the response.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

get(uri, [options], callback)

Convenience method for GET operations.

  • uri - The URI of the requested resource.
  • options - Optional config object containing settings for both request and read operations.
  • callback - The callback function using the signature function (err, response, payload) where:
    • err - Any error that may have occurred during handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

Returns an instance of the node.js ClientRequest object.

post(uri, [options], callback)

Convenience method for POST operations.

  • uri - The URI of the requested resource.
  • options - Optional config object containing settings for both request and read operations.
  • callback - The callback function using the signature function (err, response, payload) where:
    • err - Any error that may have occurred during handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

Returns an instance of the node.js ClientRequest object.

put(uri, [options], callback)

Convenience method for PUT operations.

  • uri - The URI of the requested resource.
  • options - Optional config object containing settings for both request and read operations.
  • callback - The callback function using the signature function (err, response, payload) where:
    • err - Any error that may have occurred during handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

Returns an instance of the node.js ClientRequest object.

delete(uri, [options], callback)

Convenience method for DELETE operations.

  • uri - The URI of the requested resource.
  • options - Optional config object containing settings for both request and read operations.
  • callback - The callback function using the signature function (err, response, payload) where:
    • err - Any error that may have occurred during handling of the request.
    • response - The HTTP Incoming Message object, which is also a readable stream.
    • payload - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).

Returns an instance of the node.js ClientRequest object.

toReadableStream(payload, [encoding])

Creates a readable stream for the provided payload and encoding.

  • payload - The Buffer or string to be wrapped in a readable stream.
  • encoding - The encoding to use. Must be a valid Buffer encoding, such as 'utf8' or 'ascii'.
var stream = Wreck.toReadableStream(new Buffer('Hello', 'ascii'), 'ascii');
var read = stream.read();
// read -> 'Hello'

parseCacheControl(field)

Parses the provided cache-control request header value into an object containing a property for each directive and it's value. Boolean directives, such as "private" or "no-cache" will be set to the boolean true.

  • field - The header cache control value to be parsed.
var  result = Wreck.parseCacheControl('private, max-age=0, no-cache');
// result.private -> true
// result['max-age'] -> 0
// result['no-cache'] -> true

agents

Object that contains the agents for pooling connections for http and https. The properties are http, https, and httpsAllowUnauthorized which is an https agent with rejectUnauthorized set to true. All agents have maxSockets configured to Infinity. They are each instances of the node.js Agent and expose the standard properties.

For example, the following code demonstrates changing maxSockets on the http agent.

var Wreck = require('wreck');

Wreck.agents.http.maxSockets = 20;

Events

response

The response event is always emitted for any request that wreck makes. The handler should accept the following arguments (error, request, response, start, uri) where:

  • error - a Boom error
  • request - the raw ClientHttp request object
  • response - the raw IncomingMessage response object
  • start - the time that the request was initiated
  • uri - the result of Url.parse(uri). This will provide information about the resource requested. Also includes the headers and method.

This event is useful for logging all requests that go through wreck.
The error and response arguments can be undefined depending on if an error occurs. Please be aware that if multiple modules are depending on the same cached wreck module that this event can fire for each request made across all modules. The start argument is the timestamp when the request was started. This can be useful for determining how long it takes wreck to get a response back and processed.

wreck's People

Contributors

geek avatar arb avatar cjihrig avatar totherik avatar nlf avatar hueniverse avatar kanongil avatar kidtronnix avatar lloydbenson avatar raynos avatar torsph avatar kpdecker avatar

Watchers

Peter deHaan avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.