Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending GET parameters in Restangular

I'm using Restangular in my project and earlier this code worked well for retrieving array of objects:

var params = {name: "Stack", surname: "Overflow"}
var service = Restangular.all('users')
service.getList(params)

Response from server was just an array of objects:

[
 {...},
 {...}
]

But now I added pagination, and my response now contains not an array, but an object which includes array:

{
   totalCount: 500,
   data: [
     {...},
     {...}
   ]
}

Also I changed service.getList(params) to service.get(params) (because getList expects only arrays).

And after this changes my GET parameters are not stringified, i.e. I see in debugger request like this:

users/[object%20Object]

but earlier (when using getList method) it worked as I expected:

users?name=Stack&surname=Overflow

What is the problem here?

like image 549
WelcomeTo Avatar asked Aug 25 '15 09:08

WelcomeTo


People also ask

How do I get a response from NGX-restangular?

The ngx-restangular’s “getList” and “get” methods, as an example, await a collection or an object respectively as a response by default. And in order to receive a response in a diverse format, e.g. as a string, or to specify additional parameters to the request, you can create a custom HTTP “GET” method.

How to get the response of a restangularprovider request?

For example RestangularProvider.requestParams.get = {single: true}. Supported method to configure are: remove, get, post, put, common (all) You can set fullResponse to true to get the whole response every time you do any request. The full response has the restangularized data in the data field, and also has the headers and config sent.

How do I restangular a response?

In order to get this done, you need to use the responseExtractor. You need to set a property there that will point to the original response received. Also, you need to actually copy this response as that response is the one that's going to be restangularized later

How do I set custom HTTP headers in restangular?

Please note that in order for Restangular to access custom HTTP headers, your server must respond having the Access-Control-Expose-Headers: set. You can set default Headers to be sent with every request. Send format: {header_name: header_value} If all of your requests require to send some suffix to work, you can set it here.


2 Answers

You can use customGETLIST method, look this:

// https://myapi.com/users?name=Stack&surname=Overflow

Restangular.all('users').customGETLIST('', {
    name: "Stack", 
    surname: "Overflow"
}).then(function (response) {
    console.log(response);
});

customGETLIST(path, [params, headers]): Does a GET to the specific path. In this case, you expect to get an array, not a single element. Optionally you can set params and headers.

Documentation: HERE

Good luck!!

like image 160
Sebastián Lara Avatar answered Sep 22 '22 06:09

Sebastián Lara


I was able to solve it using this:

var params = {name: "Stack", surname: "Overflow"}
var service = Restangular.all('users')
service.customGET("", params) // first parameter is required, so just provide empty string
like image 40
WelcomeTo Avatar answered Sep 19 '22 06:09

WelcomeTo