Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including fastify-http-client with all npm packages installed. Try it out:

var fastifyHttpClient = require("fastify-http-client")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

fastify-http-client v1.0.0

urllib plugin for fastify

fastify-http-client

NPM

Build Status js-standard-style codecov Known Vulnerabilities Greenkeeper badge

A plugin for Fastify that adds support for sending HTTP(s) requests.

Under the hood urllib is used, the options passed to register will be used as the default arguments while creating the urllib client.

Install

npm i fastify-http-client --save

Usage

Just add it to the project generated via fastify-cli with register in app.js as below.

You can access the urllib HttpClient instance via fastify.httpclient, which can be used to send HTTP(s) requests via methods curl / request / requestWithCallback / requestThunk. You can also send HTTP(s) requests directly via fastify.curl, which is the same function as request / curl in urllib.

'use strict'

module.exports = function (fastify, opts, next) {
  fastify.register(require('fastify-http-client'))

  // request via httpclient
  fastify.httpclient.request('https://nodejs.org/en/', (err, body) => {
    console.log('body size: %d', body.length)
  })

  // request via curl method
  fastify.curl('https://nodejs.org/en/').then((result) => {
    console.log('status: %s, body size: %d, headers: %j',
      result.res.statusCode, result.data.length, result.res.headers)
  }).catch((err) => {
    console.error(err)
  })

  next()
}

You may also supply an existing urllib HttpClient instance by passing an options object with the client property set to the instance.

'use strict'

const urllib = require('urllib')

module.exports = function (fastify, opts, next) {
  fastify.register(require('fastify-http-client'), {
    client: urllib.create()
  })

  // ...

  next()
}

Options

Besides client property sampled above, properties listed in urllib / API Doc / Method / Arguments are all supported in the options object passed to register.

License

Licensed under MIT.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free