Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set response headers

Tags:

hapi.js

I need to set some response headers within server.onPreHandler ext method. There are 2 scenarios, where I need this happen when user sends an API request to my route end point. 1) In success scenario, I need to set headers and let process continue further down life cycle 2) In error scenario (where user has not provided a required field), I need to set headers and return immediately to user with appropriate error info.

In both of these scenarios, I would like to set response headers. In the 2nd scenario above, I am able to invoke reply.response('error') and then set response header to it using response.header('x', 'value'). However, in the 1st scenario, where before calling reply.continue() I am trying to set header using request.response.header('x', 'value), I get response null error.

Please help

Thanks Ramesh

like image 628
Ramesh Mohan Reddy Avatar asked Sep 14 '26 22:09

Ramesh Mohan Reddy


1 Answers

I'm able to change response headers like this. Have you tried this way?

// at your onPreResponse ext body
const response = request.response;
if (request.response.isBoom) {
    response.output.headers['x'] = 'value';
} else {
    response.header('x', 'value');
}
like image 182
metoikos Avatar answered Sep 21 '26 02:09

metoikos