ibm-mobilefirstworklight-adapters

Get Client IP address on Worklight Adapter


We are developing hybrid application using WL6.2. In our adapter we have this code:

var request = WL.Server.getClientRequest(); 
var IPAddress = request.getHeader('X-Archieved-Client-IP');
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    IPAddress = request.getHeader('X-Archieved-Client-IP');
}
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    IPAddress = request.getRemoteAddr();
}
if(IPAddress == null || IPAddress == undefined || IPAddress == "") {
    //This will never happen, only for precaution.
    IPAddress = "192.168.1.1";
}

commonParams.originatingIp = (IPAddress)?IPAddress:request.getRemoteAddr();

I'm not able to get the client IP address on the adapter, and our web services only see one IP address which is the Data power IP.And In the logs I see our Data power IP address as "orginatingIP". which means "getRemoteAddr()" is being executed.

<OriginatingIP>DataPowerIP</OriginatingIP>

If I kept only this part of code:

var request = WL.Server.getClientRequest(); 
var IPAddress = request.getHeader('X-Archieved-Client-IP');
commonParams.originatingIp = IPAddress;

I'm getting value as "null".

<OriginatingIP>null</OriginatingIP>

I tried also this solution "IBM Worklight 6 - How would i get client IP address on adapter side" also didn't work.

any suggestions please? thanks


Solution

  • I will update your adapter as follow

    var request = WL.Server.getClientRequest(); 
    
    var ipAddress = request.getHeader('x-client-ip');
    
    switch(ipAddress){
      case "":
      case null:
      case undefined:
        //This will never happen, only for precaution.
        ipAddress = "192.168.1.1";
      break;
    }
    

    From this post https://developer.ibm.com/answers/questions/10123/how-do-you-get-the-original-ip-doing-the-request-to-your-bluemix-app.html it looks like you should use x-client-ip instead of using X-Archieved-Client-IP