Setup global Http request Headers on angular dart


How can I configure the Http service adding headers to the call.

I try the following

class GlobalHttpHeaders {
  static setup(Injector inj){
    HttpDefaultHeaders http = inj.get(HttpDefaultHeaders);
    http.setHeaders([{"X-Requested-With":"XMLHttpRequest"}], "COMMON");

And in the app the last line is:

Injector inj = ngBootstrap(module: new SiteIceiModule());

But that don’t work.


(I think) I got it working with:

class MyDefaultHeaders extends HttpDefaultHeaders {
  setHeaders(Map<String, String> headers, String method) {
    super.setHeaders(headers, method);
    //if(method.toUpperCase() == 'POST') {
    headers['X-Requested-With'] = 'XMLHttpRequest';

@NgComponent(selector: 'my-comp', publishAs: 'ctrl', template:
    '<div>My component</div>')
class MyComponent {
  Http http;
  MyComponent(this.http) {
    var h = http;
        // debugger didn't show http so I filed

    Map m = {};
    http.defaults.headers.setHeaders(m, 'GET');
    // prints:
    // {Accept: application/json, text/plain, */*, X-Requested-With: XMLHttpRequest}


class MyAppModule extends Module {
  MyAppModule() {


    type(HttpDefaultHeaders, implementedBy: MyDefaultHeaders);


I couldn’t examine http to verify the headers because the debugger didn’t show me the field but as stated in the comment
when I apply headers.setHeaders do a map inside MyComponent I get my custom header (this is what Http does with headers)
I used DI 0.0.33, Angular 0.9.9

Answered By – Günter Zöchbauer

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.