Saturday, March 6, 2010

Tunneling PUT and DELETE from ExtJS to Jersey ReST

Here is how you can invoke AJAX POSTs in ExtJS that map to PUT and/or DELETE methods in your Jersey ReST services.

The first step is to configure Jersey to allow this:
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>
</init-param>
The next step is to issue the AJAX request (in this case we're going to issue a POST but tell Jersey to invoke the PUT mapped method instead):
Ext.Ajax.request({
    headers : {
        'X-HTTP-Method-Override' : 'PUT'
    },
    method: 'POST',
    url: '/my-api/some-service',
    params: {
        name : someObj.name,
        date : someObj.date,
        amount : someObj.amount
    },
    success: this.onSaveSuccess.createDelegate(this),
    failure: this.onSaveFailure.createDelegate(this)
});

1 comment:

Jim Bikas said...

Thanks a lot RUSS!!
I was trying to implement an Ext ajax request with PUT and DELETE methods with csrf-token as a param without success for a month..
Thanks again!!!