restangular icon indicating copy to clipboard operation
restangular copied to clipboard

How to get requested url + params called by $http ?

Open ArTiSTiX opened this issue 12 years ago • 5 comments

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.

ArTiSTiX avatar Jul 29 '14 14:07 ArTiSTiX

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.

CMRahman avatar Jul 29 '14 22:07 CMRahman

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).

ArTiSTiX avatar Jul 29 '14 23:07 ArTiSTiX

use httpInterceptor or Restangular responseInterceptor

c0debreaker avatar Jul 30 '14 00:07 c0debreaker

You can use .getRequestedUrl() to get everything :).

Thanks for the question! and thanks everyone for the help answering!

mgonto avatar Aug 07 '14 00:08 mgonto

@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:

  1. function restangularizeElem(parent, element, route, fromServer, collection, reqParams) { ( https://github.com/mgonto/restangular/blob/e795cd3a39a395eac9b71febb45fa6334da37db3/src/restangular.js#L770 )

  2. Path.prototype.fetchRequestedUrl = function(current, what) ( https://github.com/mgonto/restangular/blob/e795cd3a39a395eac9b71febb45fa6334da37db3/src/restangular.js#L694 )

  3. 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.

ptanov avatar Sep 03 '15 14:09 ptanov