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

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

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.
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Sep 13, 2021 5:56 AM UTC

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!

4 replies

Asutosh_Jena_
New Participant
Sep 13, 2021 3:04 AM UTC

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! 

Translate
srinivas_chann1
Level 7
Sep 13, 2021 5:27 AM UTC

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

Translate
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Sep 13, 2021 5:56 AM UTC

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!

Translate
Fanindra_Surat
New Participant
Sep 12, 2021 3:07 PM UTC

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

Translate