Last Updated: February 25, 2016
· yannikmesserli

Post x-www-form-urlencoded data with Restangular


Sometimes REST servers want traditional "x-www-form"- urlencoded data in any requests, which looks like:


However Restangular, like any good REST frameworks will send natively the data in JSON. I had hard time to understand how I could send my data with Restangular. Note: Jquery is sending urlencoded data natively:

var data = {
     "first": "bonjour",
     "second": "salut"

    type: "POST",
    url: '',
    data: data,
    success: function() {


The first component that will help us is $.param. It will serialize our object into a string following the urlencode pattern.

var date_encoded = $.param(data); // = "first=bonjour&second=salut"

And here comes the trick. It does not appear in Restangular documentation, but .customPOST([elem, path, params, headers]) will post whatever data you pass into elem, i.e. you can set elem to be a string and it will be included into the request body. So the rest is a matter of putting the right parameters and voila, you are sending data url-encoded:

Restangular.oneUrl('myre', ''
    undefined, // put your path here
    undefined, // params here, e.g. {format: "json"}
    {'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8"}


Why not using the jquery solution? Well, because Restangular has all theses awesome features... and especially because in many cases you will need the promises that give you Restangular right out of the box. To do a promise request with jquery it's twice more code.

7 Responses
Add your response

I just ran into this issue with Django and Angular. I didn't want to write a custom solution in Angular, so I wrote a solution that allows Django to load the JSON data. This way I can just write it in plain Angular and my solution in Django allows me to just write normal Django too.

over 1 year ago ·

How to write query for where in cause in restangular for bellow url{"_id": {"$in": ["someid","someid"]}}

over 1 year ago ·

I would use:

Restangular.allUrl('search', '').getList({where: '{"_id": {"$in":["someid","someid"]}}'});
over 1 year ago ·

I'm sorry, I don't understand your question. Also, your code above misses some quotes and string concatenation operators.

over 1 year ago ·

I got my answer you are initiator for me tanks allot. My doubt is I have friends array with friends ids how can I include in above query. (["someid","someid"])

over 1 year ago ·

How to remove following syntax error:
var params = '{"keywords": {"$in":["'+($" "))+'"]}}&embedded={"author":1}'
Restangular.all('people/posts').getList({where :params}).then(function(data) {
$scope.results = data

then url showing following error :
File "<unknown>", line 1
SyntaxError: can't assign to operator

over 1 year ago ·

upto this url correctly working .
//var params = '{"keywords": {"$in":["'+($" "))+'"]}}
By adding &embedded={"author":1}' not working

over 1 year ago ·