Reputation: 119
Currently I am working with Rest API(Spring MVC). UI Part is developed in Angular JS. this project will integrate any of the domain.
For example : www.xyz.com containing my register button www.abc.com also may contain my register button.
When the request I received from user, I need to find out that, from which domain the request is coming?
Tried with the following:
@GET
@Path("/gethostname")
@Produces("application/json")
public void test(@Context HttpHeaders httpHeaders, @RequestBody JSONObject inputObj) {
System.out.println("=========> "+httpHeaders.getHeaderString("host"));
}
But it return REST API(server) Host name. How can I get client host name?
Upvotes: 4
Views: 30379
Reputation: 4084
There are two ways for you to get hostname
By calling below code on server side, please note that this will come as null if url is directly entered in browser.
String referrer = request.getHeader("referer");
Or you can provide below code to be added on client side and on server you can read the value of domain
which will return hostname
<input type="button" value="Register" onClick="call()"/>
<script>
function call(){
var domain=window.location.hostname;
window.open('http://<your-hostname>/register?domain='+domain,'_self');
}
</script>
Upvotes: 6
Reputation: 21
You can compare the Origin
httpHeaders.getOrigin()
This returns the string which will tell you the origin of the request in your case http://www.yoursampleurl.com .
Upvotes: 1
Reputation: 2872
Add HttpServletRequest request to your method definition and then use the Servlet API to get the Client remote address
@GET
@Path("/gethostname")
@Produces("application/json")
public void test(@Context HttpHeaders httpHeaders, @RequestBody JSONObject inputObj,, HttpServletRequest request) {
System.out.println("=========> "+request.getRemoteAddr());
}
Upvotes: 2