Sergey Panfilov Sergey Panfilov - 3 months ago 14
reST (reStructuredText) Question

Remove body from PUT request in ngResource query

I'm should to send a PUT query for some url.
But rest reject my query if it contains any body(JSON, like {"id": 1}).
When I'm use ngResource instead of $http, query already contains a body. How can I remove it?

Sample of expected url:

http://some.com/rest/some/1/smth?method=start


Url contains :id param and thats why my factory looks like this:

.factory('ApiResourceFactory', ['$resource', function ($resource) {
return {
payment: $resource('http://some.com/rest/some/:id/smth',
{orderId: '@id'},
{
start: {method: "PUT", 'params': {'method': "start"}},
//...
}
)
}
}])


Thats how I use it:

var id = 1;
ApiResourceFactory.payment.start({id: id}, function () {
//on success
}, function (responce) {
//on error
});


The problem is: The "id" param sends not only in url, but in request body too (request payload) as {"id": 1}.

Without body (when I'm use $http, for example), query success, but wit body server rejects my query.

So. How can I tell ngResource clear or do not fill the body?

Answer

Angular documentation of $resource is not very good, but if you look at the last example on the documentation you would get your answer. See section "Creating a custom 'PUT' request "

The way you have setup params is not correct. If you use @ then the parameter matching happens from body content. You do not need to provide params object. Also the call to start method is also slightly different.

See my fiddle here http://jsfiddle.net/cmyworld/wLQnq/

Comments