janhommes / o.js

o.js - client side oData lib.
https://janhommes.github.io/o.js/example/
MIT License
238 stars 57 forks source link

About Set Custom header in instance when get #41

Closed lllyyylll2 closed 7 years ago

lllyyylll2 commented 7 years ago

I need set custom header in my project when executing some special request. eg:

Accept : "application/vnd.ms-excel"

but I only found the global setting method .

o().config({ headers:[{ name: "Accept", value: "application/vnd.ms-excel" }]});

Can you provide an interface to do this , like changing "get()" method to add an extra prarameter named "headers" ?

janhommes commented 7 years ago

Hm, maybe we can add a .headers function which overrides the configured headers:


        // +++
        // Override the default headers. Empty restes them.
        // Example: .headers([{name: 'custome', value: 'some'}]).get().headers()
        // +++
        base.headers = function (headers) {
            if(typeof headers !== 'undefined') {
                base.oConfig['_headers'] = base.oConfig.headers; 
                base.oConfig.headers = headers;
            }
            else {
                base.oConfig.headers = base.oConfig['_headers'] || base.oConfig.headers;
            }
            return base;
        },

This peace of code is not tested. Maybe you can give it a try?