0

How can we know the name of the page or service or state of page which has called RESTful API in java file. Something like $state.current.name in AngularJS.

I have created api's in java using @RequestMapping(). In this java file I want to know about the name of page that has called this API. As I want to call same api on different pages and want to log different strings according to the page which has requested the API. I am using $http.get(api) in AngularJS to call API.

yassadi
  • 524
  • 1
  • 9
  • 20
  • Have you tried looking at the [referer header](https://en.wikipedia.org/wiki/HTTP_referer)? – Tobías May 21 '18 at 07:28
  • @Tobías I can't retrieve the complete URL using referer header. As I can only get the ip address and name of project. What I want is the content after #, i.e. state of page. – Mohit Kumawat May 21 '18 at 07:52
  • Please look [here](https://stackoverflow.com/questions/25040304/angular-ui-router-get-the-current-state-from-within-a-view?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) almost a similar question. – Sudhir Ojha May 21 '18 at 08:09
  • How about setting a cookie with the state name so that it gets included in the request? – Protozoid May 21 '18 at 08:40
  • @SudhirOjha, I want the state in the Java file, not in js file. – Mohit Kumawat May 21 '18 at 12:12
  • @Protozoid, thanks for suggestion. Actually I have already created whole project just for the sake of logs I do not want to change all the functions. Is there any direct function? – Mohit Kumawat May 21 '18 at 12:38

1 Answers1

0

We found the most reliable way to achieve knowledge of the current URL/state is to send it as part of the request payload. This gets around issues of HTTP referer header stripping. You already have $state.current.name, so adding this to your API-calling JS service function should be straightforward.

If you need to add this to every request, one option is to use an interceptor to add the metadata into every HTTP request which contains JSON.

spikeheap
  • 3,827
  • 1
  • 32
  • 47