HTTP Requests in Hapi using Wreck plugin


Wreck is Javascript library from Hapi-ecosystem to use HTTP requests. Basically it’s uses for Hapi.js projects. You can use it to write async/await code, because it’s promise-based library.


Wreck can be installed using NPM:

or YARN:

Сreating a simple HTTP request

The first argument of Wreck.request() it’s the HTTP-method. It supports all popular: GET, POST, PUT, DELETE, default is GET. The second argument – the URI of the requested resource. The third – options object. Also, we can use the next methods to make specified requests:

  • Wreck.get()
  • Wreck.put()
  • Wreck.delete()
  • Wreck.patch()

Getting JSON

To get JSON using Wreck we can use the json property of options object in .get method. We need to set it to true value:

The console will display:

To make request with query params, you can use the .payload property:

The querystring can be generated using the qs module.

Making POST request with payload

We can make POST request with JSON payload in two ways:

or using the request function:

If your app keeps the Promises-style of code, you can easily use the following:

Setting custom header

The request headers object can be set in options parameter:

Setting defaults

To make default headers, agents and other request/read objects, we can create a new instance of Wreck using the Wreck.defaults() method.

It’s useful if you need to create multiple HTTP clients with different default data.

Learning Hapi.js? Buy my Hapi.js Handbook🔥

If you liked this, please help spread the word by hitting the recommend button below. Thanks!