4

I have an angular service that looks like this. Here i am making a POST request.

.factory("Apples", function ($resource, HOST) {

            return $resource(
                HOST + "/apples",
                {},
                {
                    create: {
                        method: 'POST',
                        params: {
                            tree_id: '@treeId',
                            name: '@name',
                            color: '@color'
                        }
                    }
                }
            );
        })

The problem is that the above service makes a POST request and sends the params data as form data but also appends the params data to the url as query string. Can i avoid that?

lovesh
  • 5,235
  • 9
  • 62
  • 93

1 Answers1

6

I think you're conflicted on how to use $resource. You should create $resource instances as if they were models and set the attributes on the object that you want to POST.

With your Apples $resource defined as above:

var apple = new Apples();

apple.color = ...
apple.name = ...
apple.tree_id = ...

apple.$create()

Or you could just use the $resource class directly:

Apples.create({
    apple.color: ...
    apple.name: ...
    apple.tree_id: ...
});

Finally, $resource has the built-in $save() that uses POST which you can use instead of creating a custom $create() action.

Kevin Stone
  • 8,831
  • 41
  • 29