get the URL requested by the Angular $resource service

I use the Angular $resource service to make HTTP calls such as

var reportEndpoint = $resource('http://example.org/:id');

reportEndpoint.get({id: 6}, function (response, responseHeaders) {
    $scope.report = response.data;
    // can I get here the URL that the request was submitted to
});

at the point indicated by the comment, is there some way to get the URL that the request was submitted to, which in the example above would be http://example.org/6

Upvotes: 3

Views: 1117

Answers (2)

Sha Alibhai
Sha Alibhai

Reputation: 881

You can add an interceptor to your GET request, and attach the url to the response data. For example, you could do the following:

var reportEndpoint = $resource('http://example.org/:id', {}, {
  get: {
    method: 'GET',
    interceptor: {
      response: function (response) {
        response.data.responseUrl = response.config.url;
        return response.data;
      }
});

reportEndpoint.get({id: 6}, function (response) {
  $scope.report = response;

  // returns http://example.org/6
  console.log($scope.report.responseUrl);
});

Upvotes: 3

Ali Al Amine
Ali Al Amine

Reputation: 161

I cannot think of any direct way of retrieving the url from looking at the docs of $resource. An alternative but not optimal way to do it is by using the $httpParamSerializer to encode the data again and append that to the url to recreate the one that was used when sending the request.

Upvotes: 1

Related Questions