Skip to content

HTTP Hooks

Alexander Corn edited this page Mar 8, 2016 · 2 revisions

As of v3.19.0, all HTTP requests initiated by node-steamcommunity (and some other modules) go through a unified interface which fires pre- and post-hooks.

Pre-Hook

Due to the nature of the pre-hook, you can have up to exactly one pre-request hook. To register the pre-hook, assign a function to the onPreHttpRequest property. It should have the following arguments:

  • requestID - A unique numeric ID which is assigned to this request (will be passed to the post hook as well)
  • source - A string describing the source of this request ("steamcommunity" if it originated inside of node-steamcommunity, or optionally something else if it originated inside of another module). Might be empty string.
  • options - An object containing the request's options (including its uri and method). This is passed to the request module.
  • continueRequest - A function which you should call to either allow or block the request (see below).

If you want to defer or block this request, return true inside of the pre-hook function. Return false, something falsy, or nothing (undefined) to allow it to continue as normal.

To defer the request, return true and call continueRequest with no arguments when you want the request to continue. To block it, call continueRequest with an Error object as the first argument (this Error will be passed to the callback of whatever made the request).

You can also edit any of the properties in options.

Post-Hook

A post-hook is a bit simpler. After an HTTP request completes, node-steamcommunity will emit a postHttpRequest method with the following arguments:

  • requestID - The unique numeric request ID which was passed to the pre-hook
  • source - A string describing the source of this request ("steamcommunity" if it originated inside of node-steamcommunity, or optionally something else if it originated inside of another module). Might be empty string.
  • options - An object containing the request's options (including its uri and method). This was passed to the request module.
  • err - If an error occurred (e.g. network error, HTTP error, Steam error, trade error) then this is an Error object. null otherwise.
  • response - The response object from the request module.
  • body - The response body. If options.json is true, then this is an object. Otherwise it's a string.
  • details - An object containing metadata about the response
    • hasCallback - true if whatever initiated this request provided a callback, false if not
    • httpError - If an HTTP error occurred, this is an Error object
    • communityError - If a Steam Community error occurred, this is an Error object
    • tradeError - If a trading error occurred, this is an Error object

Making Requests

If you have a module which integrates with node-steamcommunity and you want to go through this unified HTTP interface (e.g. node-steam-tradeoffer-manager does this), then this is all you need to know:

httpRequest([uri, ][options, ][callback, ][source])

  • uri - Optional. A string containing the request URI
  • options - Optional. An object containing request options (this object will be passed to the request module)
  • callback - Optional. Called when the request completes.
  • source - Optional (but highly encouraged). A string which is passed to hooks as the source value.

All arguments are optional, but you need one of uri and options. If uri isn't provided, then either options.uri or options.url must be defined with the request URI. The options object will be passed to the request module (if provided). If you don't specify a method in options, then it will default to GET.

httpRequestGet([uri, ][options, ][callback, ][source])

Convenience method which performs a GET request.

httpRequestPost([uri, ][options, ][callback, ][source])

Convenience method which performs a POST request.