Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Inbound HTTP Headers in Loopback.io Remote Method

I have a remote method where I'm putting my application logic, as below:

module.exports = function(Entity) {
  HcpEntity.retrieveProfile = function(body, cb) {
    process.nextTick(function() {
      //TODO: Application Logic
    }
 }
}

And the corresponding model JSON snippet is:

{
    "name": "HcpEntity",
    "base": "Model",
    "properties": {},
    "methods": {
        "retrieveProfile": {
            "isStatic" : true,
            "accepts": [
                {
                    "arg": "Request",
                    "type": "object",
                    "required": true,
                    "http": {
                        "source": "body"
                    }
                }
            ],
            "returns": {
                "arg": "Response",
                "type": "object"
            },
            "http": {
                "verb": "post"
            }
        }
    }
}

I need to be able to access the incoming HTTP headers in the area marked as //TODO: Application Logic so as to validate them. Could someone please help.

like image 695
Debojit Avatar asked Dec 11 '22 10:12

Debojit


1 Answers

For the remote method accepts use -

accepts: [
    {arg: 'req', type: 'object', http: {source: 'req'}}
],

The request headers must be available in req.headers.

See: https://loopback.io/doc/en/lb3/Remote-methods.html#http-mapping-of-input-arguments

like image 61
Sterex Avatar answered Dec 12 '22 22:12

Sterex