CUSTOM HTTP HEADER AND VIRTUAL REST API

We have a virtual REST API defined for a native API and in the virtual API request processing step ( via IS service), we would like to capture the HTTP ( a CUSTOM) header name and value and do some processing. In our particular case getting a token via header and validate it against some IDP.

We tried to use pub.mediator.ctxvar:getContextVariable API to get the incoming header value - let say CUST_TOKEN and its value but not able to get it. Any advice is greatly appreciated.

Mediator and CentraSite v9.7 is being used.

Hi, your approach is correct, but you cannot access your header directly using its name. Use predefined context variable name “PROTOCOL_HEADERS” or “SOAP_HEADERS” while using pub.mediator.ctxvar:getContextVariable service.
All context variable values are typed as either “string” or “int” except for the predefined context variables PROTOCOL_HEADERS and SOAP_HEADERS, which are of the type “IData”. This IData Contains a map of key-value pairs in the request, where the values are strings. Put a tracePipeline or savePipeline after calling the getContextVariable service and see if your header is present in one of these key/value pair outcomes.

Thank you! for very quick reply.

I tried to set the pub.mediator.ctxvar:getContextVariable input varName with value as PROTOCOL_HEADERS and expecting the output to serValue (IData) from which I want to iterate to get the key/value of headers, but nothing returns to serValue from pub.mediator.ctxvar:getContextVariableservice.

When I try to debug the service - I get an error as follows

“Attempt to reference context variable without providing message context. etc”

From my experience, this header handler and requestProcessor services do not work while debugging due to MessageContext issue. Even save/restore pipeline won’t help. Please try to use “tracePipeline” service after the “getContextVariable” step and check server logs if anything gets printed for serValue?

Super. It worked. Thanks a lot.

One other question though -

Would it be possible to throw an exception to consumer ( for Virtual REST API) when a certain condition is not met request processing service? Basically, I am checking for a token in the request processing step and when this token is not valid I want to stop right there and throw an exception to the user. Please advise.