Reputation: 171
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
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
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