5

I have this request:

$http({
  method: 'get',
  url: '/api/items/',
  params: {a: 1, b: 2, c: 3}
}).success(function (response) {
  console.log(response);
}).error(function (err) {
  console.log(err);
});

Is there a way, to fetch the url it used to make the request after the request has been made(in the callback or otherwise)?

I would want the output:

http://www.example.org/api/items?a=1&b=2&c=3

Here the same thing is done with jquery.

2
  • It's not working using params ? Commented Jul 6, 2015 at 11:43
  • $location.search('target') seems to not work even in promise success function. Commented Jul 6, 2015 at 12:09

4 Answers 4

2

The success handler gets 4 parameters passed into it:

$http
  .get({ url: '/someUrl', params: { q: 3 } })
  .success(function(data, status, headers, config) {});

The fourth parameter config has the following properties:

{
  method: "GET",
  url: {
    url: '/someUrl',
    params: {
      q: 3
    }
  }
}

You can use window.location.origin to get the base url and build a simple function to concat it all together.

This function should yield the expected response:

var baseUrl = window.location.origin;
var query = [];
Object.keys(config.url.params || {}).forEach(function (key) {
  var val = config.url.params[key];
  query.push([key, val].join('=')); // maybe url encode
});
var queryStr = query.join('&');
var fullPath = baseUrl + config.url.url + '?' + queryStr;

Unfortunately, this function will only work as long as the parameters are passed in the format described above. If you pass them in a different format, you'll have to modify this function a bit. You can use this as a playground.

Afaik, there is no simpler way. At least a feature request exists.

See the docs of $http for reference

Sign up to request clarification or add additional context in comments.

5 Comments

that url gives me only the path without the parameters
Check this example - the url contains the query parameter as well.
the example is working because params are attached to the url (/url?q=3). But in my case, params are given separately (params: {q:3})
is the function working for you? I get config.url.params as undefined and it is giving me this error Cannot convert undefined or null to object in forEach part. Thanks for trying to help though.
The function above will only work as long as you pass the parameters in the described format. If you pass them in a slightly different format, you'll have to modify the function.
0

This config parameter of the callback has the url as a property:

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    console.log(config.url);
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

Based on: https://docs.angularjs.org/api/ng/service/$http

1 Comment

that would give me only /someUrl without params
0

You can use http request Interceptor. Configure the Interceptor in config function using $httpProvider Whrein for request Interceptor you can able to see the URL and params that you passed with URL prior to making the request

1 Comment

can you share the code here. how to get the URL and param inside $httpInterceptor
0

try just looking at the XHR Request urls in your browser dev tools

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.