post parameter encoding in request body using angularjs $resource

8,015 views
Skip to first unread message

Ali

unread,
Aug 11, 2012, 9:41:47 AM8/11/12
to ang...@googlegroups.com
Hi All, 

 How can I want to use angular $resource to send a post request which has request parameters encoded in the request body rather than being part of url.

When I try 

$resource("/data").save({'p':'foo'})
 ...

On the server side, the output of request.body is

{"p":"foo"} 

Apparently the parameter is encoded as JSON rather than being encoded in the body of the request as 

p=foo

How can I correctly post/encode key value pairs for post requests using $resource.

I tried 

$resource("/data",{'p':'foo'}).save ...

This encodes the parameter in url although I used save which implies post request. I want to only have the parameters encoded in body of the request and the url remain the same (/data).


Thanks,
Ali

super...@gmail.com

unread,
Sep 10, 2012, 6:33:16 PM9/10/12
to ang...@googlegroups.com
Sorry for bumping but I'm also interested in a solution to Ali's question. I have tried the latter method but as Ali has already pointed out, the parameters are encoded in the URL not in the body of the request.

Dan Helyar

unread,
Mar 28, 2013, 11:59:19 AM3/28/13
to ang...@googlegroups.com, super...@gmail.com
Also bump! I would have thought this would be such a common requirement for many Angular apps. Strange that information on it seems so sparse.

Dan Helyar

unread,
Mar 28, 2013, 11:15:57 PM3/28/13
to ang...@googlegroups.com, super...@gmail.com
Well I know this doesn't solve the question, and maybe it's not adequate, I'm too new to Angular to know, but I solved the issue by switching to $http such as:

var LoginCtrl = function ($scope, $http) {
    $scope.loginSubmit = function () {
        $http({
            url: jsurl('profile.login'),
            method: 'POST',
            data: $.param({
                username: $scope.username,
                password: $scope.password,
                csrfmiddlewaretoken: $.cookie('csrftoken')
            }),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).
        success(function (data) {
            $scope.success = true;
        }).
        error(function (response) {
            data = angular.fromJson(response)
            $scope.success = false;
            $scope.errors = data.errors;
        });
    }
}

Sameer Ranawade

unread,
Jun 2, 2013, 8:34:40 AM6/2/13
to ang...@googlegroups.com
Bump! could anyone please give a solution to this issue... sending parameters using POST through request body, instead of url.

Jose Luis Rivas

unread,
Jun 2, 2013, 1:17:02 PM6/2/13
to ang...@googlegroups.com
http://docs.angularjs.org/api/ng.$http#post

Second parameter is `data`. That's the payload of the request. POST does not send data on the URL.

On Jun 2, 2013, at 8:04 AM, Sameer Ranawade <rana...@gmail.com> wrote:

> Bump! could anyone please give a solution to this issue... sending parameters using POST through request body, instead of url.
>
> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Dan Helyar

unread,
Jun 2, 2013, 10:13:03 PM6/2/13
to ang...@googlegroups.com
I found a better solution that involves setting the httpDefault in my initial app definition, e.g.:

        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

This configures all $http POST and by inheritance I believe, all $resource POST requests to send the data payload in the request body instead.

I believe there is also a way to do this by setting the header individually in the $resource, however I'm not sure whether this feature is in a stable release yet, nor have I tried it.
Reply all
Reply to author
Forward
0 new messages