Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the HTTP Accept header for JsonRestStore

I'm using JsonRestStore but would like to add a custom Accept header to it. What's the best way to go about this? This is similar to how the dijit.layout.ContentPane allows you to affect the underlying XHR by setting ioArgs. So the question could be "what is JsonRestStore's ioArgs?"

I'm using declarative syntax, but would gladly like to see both methods...

(Please note: I'm not interested in hacking my way around this by modifying the base XHR.)

like image 633
Alexander Trauzzi Avatar asked Nov 15 '22 03:11

Alexander Trauzzi


1 Answers

Your best bet is providing a custom service to JsonRestStore. The easiest way I found of doing this is building the service from dojox.rpc.Rest. In the constructor you can provide a function to create the request arguments for all XHR requests. E.g.

function getRequest(id, args) {
    return {
        url: '/service/' + id,
        handleAs: 'json',
        sync: false,
        headers: {
            Accept: 'your custom header'
        }
    }
}

var service = new dojo.rpc.Rest('/service/', true /*isJson*/, 
                                undefined /*schema*/, getRequest);
var store = new dojox.data.JsonRestStore({ service: service });

This completely ignores the args parameter that can include sorting and range arguments to your service.

These links will provide more information:

  • Use Dojo's JsonRestStore with your REST services: IBM developerWorks article with a more advanced and customizable solution
  • RESTful JSON + Dojo Data: Sitepen post
  • dojox.rpc.Rest source file (look for service._getRequest)
like image 60
Gustavo Giráldez Avatar answered Dec 09 '22 20:12

Gustavo Giráldez