How to pass an array of object as parameters in Restangular?

Issue

I have no idea how i can pass an array of object in Restangular. I’ve read their documentation. I found that they provided such as customGET, customPOST etc. But, i didn’t see the right example that related to my case. For now, i want it to get data from an API that needs params as its filter.

1) Params

var filter = {
  category: 1,
  page: 1,
  product: 20,
  price_range: ['bt',1,150]
}

2) Services

getRawList: function(filter) {
   return rawProducts.customGET('',filter).then(function(response) {
      return response;
   });
},

What i got was an Internal Server Error. Any idea how to tackle this error ?

Solution

When sending data to a web server, the data has to be a string. So, on this situation i need to convert the array property to string (which is price_range) before send it to the server as filter. This code solved my question.

getRawList: function(filter) {
   return rawProducts.customGET('',{
              category: filter.category,
              page: filter.page,
              product: filter.product,
              price_range: JSON.stringify(filter.price_range)
          }).then(function(response) {
      return response;
   });
}

Answered By – Rido

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.