hapi — Access Request Headers

Handling request headers isn’t a use case that comes to mind immediately when thinking through the development process of your app. You might think of routes, requests, appropriate responses and the design of your views. And at some point, you’re in a more advanced development stage and see the benefit of using request headers for a given task.

In this tutorial, you’ll learn how to access and handle HTTP headers that are sent with your request.

Before moving on with request headers in hapi, have a look at other tutorials within this in-depth series.

hapi Series Overview

Access Request Headers

Every request that touches your server contains a ton of client information, like HTTP method, the request URL, payload, query and path parameters.

Further, the request also includes HTTP headers that are of interest in specific situations, like pagination or checking if the request went through a (reverse) proxy, like nginx. Well, there are multiple scenarios where request headers are a very suitable use case to transmit data with the client request. For example, GitHub uses request headers to allow developers paginate through individual API endpoints.

Alright, the following snippet illustrates how to get access to the request headers in hapi.

hapi v17

server.route({  
  method: 'GET',
  path: '/',
  handler: (request, h) {
    var headers = request.headers   // <-- this is the important line

    // use of individual header values for data processing

    return { your: data }
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    var headers = request.headers   // <-- this is the important line

    // use of individual header values for data processing

    reply({ your: data })
  }
})

The snippet is reduced to the route definition and the interesting part is the route handler. Within the route handler, you’ve access to the request object and that includes the headers.

request.headers returns an object of key-value-pairs, like

{ 
  host: 'localhost:3000',
  connection: 'keep-alive',
  'cache-control': 'max-age=0',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) …',
  'accept-encoding': 'gzip, deflate, sdch, br'
}

As you can see, the keys differ in their form, like the first two (host and connection) are without quotes and the others is wrapped in single quotes. The quotes are required due to the dashes in-between the key’s words.

You can access individual fields like this:

var host = request.headers.host  
var cacheControl = request.headers[ 'cache-control' ]

// do further processing with the host and cache control values

That’s all the magic behind request headers in hapi.

Notice: you need to keep an eye on the format of the header key and depending on wrapping quotes, you need to access the related value differently.

Outlook

This tutorial walked you through the handling request headers and showed you that individual keys may be formatted differently. Further, you know how to access the values of headers fields, even though they are or aren’t wrapped in quotes.

Have a question or thought in mind? Please don’t hesitate to leave a comment or tweet us @futurestud_io.

Make it rock & enjoy coding!

Explore the Library

Find interesting tutorials and solutions for your problems.