How is it possibile to access the request headers inside an operation hook?
I have a model that I have exposed through the REST API, and everytime I access an api I need to check whether a header field is present in the request or not, but apparently this is not possible. Well, at least, I haven't found any solution.
For instance, I have created a model named Shop and made it public through REST.
When I compose a GET request to localhost:3000/api/shops to get the values, I want to check whether a particulare field is present in the header or not, so that I can take actions based on it.
I have created a operation hook on the Shop model as follows:
module.exports = function(Shop) {
Shop.observe('access', function(ctx, next) {
// DO SOMETHING WITH THE HEADER
next();
});
};
But i haven't found any way to access the http headers from inside this operation hook.
How do I do this?
hi @macogala, have you looked at https://loopback.io/doc/en/lb3/Using-current-context.html#access-the-context-from-operation-hooks ?
it's been recently added to LoopBack and should match your needs.
Note: it requires LoopBack v3
hi @ebarault , I have looked at it, but if I perform a GET request , ctx does not contain the header of the request, just an undefined accessToken. Yes, I am using Loopback 3.0.
To test it, I created a GET request with Postman and inserted a field in the header named Authorization and its value, but I can't see it in the ctx object. The same problem should anyway occur with any other header field. It just looks like I can't access the header from inside the hook. Am I wrong? Do you have a different behavior?
@macogala : using the context params in req options require a little bit of configuration, as mentioned here
did you go through the process of customizing the value provided to the “options” object?
for example, the following should give you access to the full ctx inside the options object:
MyModel.createOptionsFromRemotingContext = function(ctx) {
var base = this.base.createOptionsFromRemotingContext(ctx);
return extend(base, {
fullCtx: ctx,
});
};
@ebarault using the above code returns:
ReferenceError: extend is not defined
at Function.Document.createOptionsFromRemotingContext (.../server/models/document.js:35:12)
at createOptionsViaModelMethod (.../node_modules/loopback/lib/model.js:496:22)
at HttpContext.buildArgs (.../node_modules/strong-remoting/lib/http-context.js:139:17)
at new HttpContext (.../node_modules/strong-remoting/lib/http-context.js:59:20)
at restStaticMethodHandler (.../node_modules/strong-remoting/lib/rest-adapter.js:426:15)
at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
at next (.../node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (.../node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
at .../node_modules/express/lib/router/index.js:281:22
at Function.process_params (.../node_modules/express/lib/router/index.js:335:12)
at next (.../node_modules/express/lib/router/index.js:275:10)
at Function.handle (.../node_modules/express/lib/router/index.js:174:3)
at router (.../node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (.../node_modules/express/lib/router/index.js:317:13)
at .../node_modules/express/lib/router/index.js:284:7
at Function.process_params (.../node_modules/express/lib/router/index.js:335:12)
at next (.../node_modules/express/lib/router/index.js:275:10)
at jsonParser (.../node_modules/body-parser/lib/types/json.js:112:7)
at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (.../node_modules/express/lib/router/index.js:317:13)
I like the idea of "extend", can you let me know how to use 'extend' in this situation?
@honestserpent I did the following to include ctx (I only needed ctx.req) inside my operation hook:
_MyModel_.createOptionsFromRemotingContext = function (ctx) {
var base = this.base.createOptionsFromRemotingContext(ctx)
base.ctx = ctx.req
return base
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
Most helpful comment
@ebarault using the above code returns:
I like the idea of "extend", can you let me know how to use 'extend' in this situation?
@honestserpent I did the following to include ctx (I only needed ctx.req) inside my operation hook: