Shannon Hochkins
Shannon Hochkins

Reputation: 12175

Using $resource and creating a POST request?

I've been digging at this but cannot figure this one out, here's the simple attempt I've made, I thought this was fine but it seems to still request as get....

this.request = function(url, requestData) {
    return $resource(url, null, {
        post : {
            method : 'POST',
            params : requestData || {}
        }
    });
};

Using it:

this.request('/some/api/url', {data : true}).post();

I can't seem to figure out how to get back a promise object so that I can use the repsonse data....

Upvotes: 0

Views: 214

Answers (1)

Simon K
Simon K

Reputation: 2857

You want to create your resource like so:

$resource(url, null, {
    post: {
        method: 'POST'
    }
});

And then:

this.request.post(
    requestData,
    function (successResponse) {
        // Do whatever with response
    },
    function (failResponse) {
        // Do whatever with response
    }
);

This will send a POST request to url with requestData as the body.

Upvotes: 1

Related Questions