Send Request Body on $resource
Asked Answered
G

2

37

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.

Goulet answered 24/4, 2013 at 22:58 Comment(0)
B
30

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/

Boycie answered 24/4, 2013 at 23:7 Comment(5)
Thanks! I was thinking that only parameters inside the {} are acceptable.Goulet
This saved me as well....I'm working with ServiceStack trying to POST ($resource.save()) a complex object which $resource serialized to query string parms which works fine for SS to deserialize flat objects but clearly not complex ones. Your fiddle saved me!!Palais
I'm really confused by the jsFiddle you posted. If I look at chrome debugger, the invoked method is a GET over the wire. Doesn't angular make that a POST by default for save?Rillet
@KenPerkins I see a POST first and then a GET. It seems that jsFiddle responds with a 301 to the dummy POST request.Boycie
I think you're right. Chrome Inspector appears to be masking the initial POST :SRillet
E
41

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

myResource.save({ param: myParam }, myObject);
Eft answered 19/7, 2014 at 2:55 Comment(0)
B
30

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/

Boycie answered 24/4, 2013 at 23:7 Comment(5)
Thanks! I was thinking that only parameters inside the {} are acceptable.Goulet
This saved me as well....I'm working with ServiceStack trying to POST ($resource.save()) a complex object which $resource serialized to query string parms which works fine for SS to deserialize flat objects but clearly not complex ones. Your fiddle saved me!!Palais
I'm really confused by the jsFiddle you posted. If I look at chrome debugger, the invoked method is a GET over the wire. Doesn't angular make that a POST by default for save?Rillet
@KenPerkins I see a POST first and then a GET. It seems that jsFiddle responds with a 301 to the dummy POST request.Boycie
I think you're right. Chrome Inspector appears to be masking the initial POST :SRillet

© 2022 - 2024 — McMap. All rights reserved.