hostname or ip address of the system we are triggering a request (aem6.5) | Community
Skip to main content
srinivas_chann1
Level 7
September 12, 2021
Solved

hostname or ip address of the system we are triggering a request (aem6.5)

  • September 12, 2021
  • 2 replies
  • 1724 views

Hi,

 

I will be uploading an  image to aem server using a service that aem or coustom code provides (www.abc.com/upload/image.json)

 

Now this request is triggered from https://uploadservice.com . How could i get the host name in aem servlet (uploadservice.com) and ip address of this when it is triggred from (uploadservice.com).

 

What will code for this??

 

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

What is this request? How will it invoke the above code if you access the URL.

Can you please provide info how are you integrating this code?

 

Thanks!

2 replies

Fanindra_Surat
Community Advisor
Community Advisor
September 12, 2021

Hi @srinivas_chann1 ,

 

Host name - Host name can be obtained from the SlingHTTPServletRequest object header "referer". Sample - 

request.getHeader("referer");

 

IP address - There are few methods that help here like getRemoteAddr()/getRemoteHost() of SlingHTTPServletRequest object or the request header value "X-Forwarded-For". But, they can't be entirely relied on, because for most of the scenarios you may be receiving the IP address of the Default Gateway or proxy.

 

Thanks,

Fani

Asutosh_Jena_
Community Advisor
Community Advisor
September 13, 2021

Hi @srinivas_chann1 

 

Please use the below code. I use it and it works perfectly fine.

 

public static String getClientIPAddress(HttpServletRequest request) {
String xForwardedForHeader = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
if (xForwardedForHeader == null) {
return request.getRemoteAddr();
} else {
return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
}
}

 

Thanks! 

srinivas_chann1
Level 7
September 13, 2021

Hi ,

I tried it my local with the above code using postman  to trigger the reuqest( http://localhost:4502/upload/image.json) .In the logs it does not print any value for this ??

 

What could be the reason

 

Thanks

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
September 13, 2021

What is this request? How will it invoke the above code if you access the URL.

Can you please provide info how are you integrating this code?

 

Thanks!