Question

Getting X-FORWARDED-FOR details from Http Request

Hi,

I am trying to get the X-Forwarded-For header details from the user login request in my PRPC personal editions (Pega 8.1). I added the following code to ApplicationProfileSetup activity. I am getting the "request" as Null, hence it is not getting into the if condition. Can you any one suggest, what might be going wrong in the below code?

javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest)tools.getRequestor().getRequestorPage().getObject("pxHTTPServletRequest");

oLog.error ("TEST LOG");

if (request != null) {

String ivuser = request.getHeader("X-Forwarded-For");

oLog.error("This is the request header" + ivuser );

}

Thanks

Abdul Rasheed

Correct Answer
September 25, 2019 - 6:00pm

Finally I am able to fix this with following code :-)

javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest)tools.getRequestor().getRequestorPage().getProperty("pxHTTPServletRequest").getObjectValue();

 

Comments

Keep up to date on this post and subscribe to comments

September 25, 2019 - 6:41am

Can you please turn on debug log 'LogHttpRequest' (through Admin Studio using Log categories) and share the log?

Pega
September 25, 2019 - 8:41am

Hi,

Your code is correct. The header may not be there so you are getting a null result. 

Try outputting all the headers we are receiving. 

java.util.Enumeration e = request.getHeaderNames();
String header = null;
while (e.hasMoreElements()) {
      header = (String) e.nextElement();
      oLog.infoForced(header + ": " + request.getHeader(header));      
}
 

We also have a logger called logHttpRequest that will output all incoming header and cookies. If you enable that to debug you can get the same thing. 

Hope this helps,

--Chris

 

September 25, 2019 - 11:29am

Please find the logs attached.  I see the X-Forwarded-For header is getting printed in Log files.  Not sure why I am not able to get that in Activity.

September 25, 2019 - 6:00pm

Finally I am able to fix this with following code :-)

javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest)tools.getRequestor().getRequestorPage().getProperty("pxHTTPServletRequest").getObjectValue();

 

Mod
September 26, 2019 - 1:00am
Response to AbdulRasheedS

Glad to hear that you found the solution! We will mark your post as Answered.

Lochana | Community Moderator | Pegasystems Inc.