2

I have two services:

var PlanResource = $resource('/api/v1/plan/:planId', {}, {
  'update':  { method:'PUT', params:{ planId : '@_id.$oid' }, isArray : false },
});

and

var UserResource = $resource('/api/v1/user', {}, {
    'update': {method:'PUT', params:{ }, isArray : false},
});

When I call update on the first it puts the data I pass in the body where as the second puts the data in the URL for example:

PUT /api/v1/user?profiles=%7B%22skeleton%22:false,%data%22:234

Why does this happen, and how can I get the second to put the data in the body?

EDIT:

Usage is:

ctrl.user.$update({'profiles' : appCtrl.user.profiles}).then(function() {

and

ctrl.plan.$update({name: planName});
3
  • usually means the contentType is not set properly... can you check the request in the network tab of browser developer tools and check the request headers Commented Oct 18, 2014 at 4:24
  • Both have: Content-Type:application/json;charset=UTF-8 Commented Oct 18, 2014 at 4:28
  • There aren't any parameter it's to update the currently logged in user. So there is only one. Commented Oct 18, 2014 at 4:30

1 Answer 1

4

Can you show the code of your usage?

Generally, you should not put any value that you intend to put in the body into the "params". They should belong to a separate argument. In the $resource document, there is

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

So you should try sth like:

UserResource.update({
    // URL params here.
}, profiles   // Body here.
, ..., ...);

EDIT:

As you updated, you are using instance methods instead of class methods. In the doc, it says:

non-GET instance actions: instance.$action([parameters], [success], [error])

In an instance method call, you can only pass in URL parameters. The body of the request would automatically be the object itself (in your case, ctrl.user would be the body).

If you really want to modify the request body to something other than the object itself, please use the class methods instead.

The doc for $resource is here:

https://docs.angularjs.org/api/ngResource/service/$resource

Sign up to request clarification or add additional context in comments.

1 Comment

I added the way it's called

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.