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?
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.
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.
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
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.
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!!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With