cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to add request headers to approuter

nathanhand1
Explorer
0 Kudos

Hiya,

I've been trying without success today to add custom headers to a certain destination/ API call in my sap ui5 application router.

Below commnet is just one example of me trying different combinations of headers/ setting headers without success.

This really should be possible based on this git repo example too.


I know my app router is using this custom file because I put in a different response test and that worked fine.


What's the correct way to insert custom headers to each request? I did also try on the destination level but this didn't work either!

Many thanks,

Nathan Hand

nathanhand1
Explorer
0 Kudos
var approuter = require('@sap/approuter')
var ar = approuter()

ar.beforeRequestHandler.use("^/api", function myMiddleware(req, _res, next) {
req.headers['APIKey'] = "123456";
req.headers.APIKey2 = 123456;
req.headers.APIKey3 = "123456";
next()
});

ar.start();
nathanhand1
Explorer
0 Kudos

Additionally as mentioned things like the following didn't work, or other combinations of URL's to attach to:

ar.beforeRequestHandler.use(function myMiddleware(req, _res, next) {
req.headers['APIKey'] = "123456";
req.headers.APIKey2 = 123456;
req.headers.APIKey3 = "123456";
next()
});

Accepted Solutions (0)

Answers (1)

Answers (1)

mariusobert
Developer Advocate
Developer Advocate
0 Kudos

You actually did everything right. Which version of the approuter are you using?

nathanhand1
Explorer
0 Kudos

We're using "@sap/approuter": "9.4.0" which checking isn't the latest version, so I just did a quick update and deploy with 10.10.2 with no difference.

mariusobert
Developer Advocate
Developer Advocate
0 Kudos

Are you sure you send the token in the right format? Maybe you can try to redirect the traffic to a local web server (as destination) to see which headers are actually forwarded to the destination.

mariusobert
Developer Advocate
Developer Advocate
0 Kudos

And make sure if this callback function is really called (i.e. add a "console.log('foo')") to see if the code is executed at all.

nathanhand1
Explorer

I took a few days off this problem and came back to it today, it seems that the "^" doesn't work/ apply here so you need to use the full path.

After I changed the path to "/appname/api/" this worked fine as that's how my apps are calling the API as far as the app router is concerned.

Silly mistake but thanks for being a sounding board and helping me fully check into the problem.