How do I not send url template parameters with request body in angular?
Asked Answered
G

2

21

Suppose I have a resource set up like this:

resource = $resource(
    "http://foo.com/service/:type/:id",
    {},
    {save: {method:'PUT', params: {type:'@type', id: '@id'}}}
);
resource.save({type:'user', id:14, name:'Bob Dole'});

Is there any way I can prevent type and id from being submitted as part of the request body, and just send name in the PUT payload? I don't control the API I am submitting to, and it seems to not like the extra parameters I am sending it.

Thanks!

Update - 10/25/13 - 13:38

The documentation for resource says this:

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

That implies that this ought to remove the parameters from the data:

resource.save({type:'@user', id:'@14', name:'Bob Dole'});

but it doesn't seem to work. Still at a loss.

Genuflection answered 22/10, 2013 at 22:25 Comment(3)
This is a big-time guess; but When using $HTTP; you can add a transform function to modify the request. Can you do the same thing with $resource?Tweeze
Thanks - that does work! See above for my solution. However, the docs mention an easier way that ought to work, but does not.Genuflection
You should post your solution as a formal answer [AKA Answer your own question]. It'll help people who find in the future.Tweeze
G
7

FWIW, I did find a workaround, thanks to @Reboog711, by including a transformRequest parameter like so:

resource = $resource(
    "http://foo.com/service/:type/:id",
    {},
    {save: {
        method:'PUT', 
        transformRequest:function(data) {
            delete data.type;
            delete data.id;
            return JSON.stringify(data);
        },
        params: {type:'@type', id: '@id'}
    }}
);
Genuflection answered 27/10, 2013 at 23:25 Comment(0)
P
33

Use the first parameter for your url template parameters and put your post data in the second parameter like this:

resource.save({id:14, type:'user'}, {name:'Bob Dole'});

Here's the line from the Angular docs that shows the function signature:

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

Here's an example in plunker

The request you get does not have the url parameters in the body:

Request URL:http://run.plnkr.co/JAOqZqW6RSywatUM/badUrl/user/14
Request Method:PUT
Request Payloadview source
{name:Bob Dole}
Proprietor answered 9/7, 2014 at 16:34 Comment(0)
G
7

FWIW, I did find a workaround, thanks to @Reboog711, by including a transformRequest parameter like so:

resource = $resource(
    "http://foo.com/service/:type/:id",
    {},
    {save: {
        method:'PUT', 
        transformRequest:function(data) {
            delete data.type;
            delete data.id;
            return JSON.stringify(data);
        },
        params: {type:'@type', id: '@id'}
    }}
);
Genuflection answered 27/10, 2013 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.