How to get requested url + params called by $http ?
I need to obtain the URL, and the passed request params which is called by $http through Restangular.
I usually do:
Restangular.all(mymodel).getList(myparams).then(...)
But now I want to obtain the corresponding URL + query params, instead of playing it.
Use case: User fills a form, with many filters, and want to choose between playing the request, or getting the url to get it himself.
I'm not quite sure about the scenario you mentioned. But I'm guessing that you want to check the attributes on the Request before you send it to the server. If that is indeed your intention, then please check the addRequestInterceptor. If you need even more granular control (ex. setting headers, params etc) you can use the setfullrequestinterceptor.
To make a getList(), I use the line above.
I need a kind of method like getRequest, or get RequestedUrl() (this method exists but only returns URI part, not the query parameters) giving for example:
/myapi/v1/potatoes/?taste=good
For the moment, i used jQuery param() function to encode query params, and Restangular all().getRequestedUrl() method for URL. But i would have prefered not to depend on an external library, as $http must have also a ObjectToQueryParams method.
The only thing i need, is to easily get request object (and the formatted params done by $http, as another library could do it differently), and why not... headers (i don't need it, but someone could), preferably before sending it (or without sending it).
use httpInterceptor or Restangular responseInterceptor
You can use .getRequestedUrl() to get everything :).
Thanks for the question! and thanks everyone for the help answering!
@ArTiSTiX , I know what you mean. @mgonto is talking about the case where the elements have been previously gotten from the server whereas you're talking about constructing an URL without getting the element from the server.
I have faced the same problem and the solution I have found is to explicitly add the request parameters (there is one more solution at the bottom of this comment):
var someUrlParams = {hello: "world"};
var endpoint = Restangular.all("someroute");
endpoint[Restangular.configuration.restangularFields.reqParams] = someUrlParams;
endpoint.getRequestedUrl()
this will return /someroute?hello=world. I think this is what you expect.
Long story: in https://github.com/mgonto/restangular/blob/e795cd3a39a395eac9b71febb45fa6334da37db3/src/restangular.js config.restangularFields.reqParams is referenced in these two lines:
-
function restangularizeElem(parent, element, route, fromServer, collection, reqParams) {( https://github.com/mgonto/restangular/blob/e795cd3a39a395eac9b71febb45fa6334da37db3/src/restangular.js#L770 ) -
Path.prototype.fetchRequestedUrl = function(current, what) (https://github.com/mgonto/restangular/blob/e795cd3a39a395eac9b71febb45fa6334da37db3/src/restangular.js#L694 ) -
is the code that is invoked by
.getRequestedUrl()so the only place that params are implicitly set is in 1)
restangularizeBase is referenced in two places - restangularizeElement() and restangularizeCollection() - and the only place where reqParams (the last argument) is passed to restangularizeElement() is in okCallback() - when the element has actually been gotten from the server.
So the second workaround (first is endpoint[Restangular.configuration.restangularFields.reqParams] = {hello: "world"};) is to use Restangular.restangularizeElement() and to pass req params to the empty element.