Written by

CTO at VOCAST
Question Brian Palmund · Aug 9, 2016

How to get custom HTTP request headers

I cant find any description on how to read custom http request headers in a REST service.

On some occations i want to read http headers passed along when customers uses our REST API or headers added by the CND provider. Does anyone know how to read these http headers?

For example:
cloudflair.com sends a vistors country information along to the server using a customer http header called "CF-IPCountry". We would like to read this information and use it for statistics.

Comments

Dmitry Maslennikov · Aug 10, 2016

You can do it as in any CSP page/class. With %request, which is object of class %CSP.Request

And headers available in property CgiEnvs, where every http header appear with a prefix HTTP_

So, you may try this code.

set ipCountry=%request.GetCgiEnv("HTTP_CF-IPCOUNTRY")
0
Brian Palmund  Aug 10, 2016 to Dmitry Maslennikov

Perfect! It works like a charm....

The documentation on CGI Environment Variables only refers to a set of variables to available. I does not describe that all http headers is readable thru HTTP_<header>.

Thanks!

/Brian

0
Brian Palmund  Aug 10, 2016 to Dmitry Maslennikov

One little detail. Any dash (-) is converted to underscore (_) so the correct code in this example is:

set ipCountry=%request.GetCgiEnv("HTTP_CF_IPCOUNTRY")

for a header called CF-IPCOUNTRY.

0