restangular icon indicating copy to clipboard operation
restangular copied to clipboard

How to send complicated request with getList? (POST instead of GET)

Open demkinmaxim opened this issue 12 years ago • 1 comments

Hello! I'm using getList to fetch list of my items. I use server-side pagination, sorting, filtering. I specify queryParams from getList and id send it with GET, like ?sorting=created&page=3 But my filter and search params could be more complicated, like filter: { group_id: 3, created: '>2013-01-01', user_id: [1,4] } or like multiline search (user inputs several line of text for search).

Without Restangular I handle it simply with POST request, but I did not find how to switch from GET to POST. Surely it's not like REST should behave. POST to collection is meant for creating a new element, but... I still need to send such requests to server. Maybe there is some way I've not found?

Thanks in advance!

demkinmaxim avatar Feb 07 '14 04:02 demkinmaxim

Hey.

If you need to send that as a POST, you can do a customPOST on that, and then just manually restangularize the collection.

Something like (It's not 100% compilant but it's so that you get the idea):

Restangular.all('places').customPOST({filter: {group_id: 3}}, 'search').then(function(response) {
  var list = Restangular.restangularizeCollection(null, response, 'places');
})

In this case, it'll do a POST to /places/search with body of filter.

If you don't mind if the list isn't restangularized, you can just set the response to the scope and that's it :).

Hope it helps!

mgonto avatar Feb 07 '14 05:02 mgonto