uqttoa
Last Updated: February 25, 2016
·
12.14K
· yannikmesserli

Post x-www-form-urlencoded data with Restangular

Problem

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

first=bonjour&second=salut

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"
};

$.ajax({
    type: "POST",
    url: 'http://myre.ch',
    data: data,
    success: function() {
        //...
    }
});

Solution

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', 'http://myre.ch'
.customPOST(
    date_encoded, 
    undefined, // put your path here
    undefined, // params here, e.g. {format: "json"}
    {'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8"}
);

Reason

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

15061

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. https://coderwall.com/p/mwhmfg?i=1&p=1&q=author%3Alucasroesler&t%5B%5D=lucasroesler

over 1 year ago ·
18008

How to write query for where in cause in restangular for bellow url

http://127.0.0.1:8000/?where={"_id": {"$in": ["someid","someid"]}}

over 1 year ago ·
18010

I would use:

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

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

over 1 year ago ·
18021

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 ·
18040

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

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

over 1 year ago ·
18041

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

over 1 year ago ·