Send Request Body on $resource

I take a look on Angular API for $resource and I didn’t find some way to send a Request Body to a RESTful service.

I know this is possible using $http approach, like here, so, is it also possible to do using $resource?

Apparently this is the options for $resource.

action – {string} – The name of action. This name becomes the name of the method on your resource object.

method – {string} – HTTP request
method. Valid methods are: GET, POST, PUT, DELETE, and JSONP

params
{object=} – Optional set of pre-bound parameters for this action.

isArray – {boolean=} – If true then the returned object for this
action is an array, see returns section.

At the moment I didn’t found any way to send a request payload containing an JSON object.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Building on @gargc‘s answer, you can pass parameters and a body to a resource’s method:

myResource.save({ param: myParam }, myObject);

Solution 2

As long as the action supports a body message (e.g. POST, but not GET), you can pass a data parameter to the action method of your resource, which will be sent in the body of the request:

yourResource.save(yourData)

Example: http://jsfiddle.net/N3NXK/1/

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply