Configuring a global Http request Headers in angular dart

How to configure the Http service to add headers to the call.

I will try the following

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

And in the application, the last line:

Injector inj = ngBootstrap(module: new SiteIceiModule());
  GlobalHttpHeaders.setup(inj);

But that does not work.

+3
source share
3 answers

(I think) I worked with:

@Injectable()
class MyDefaultHeaders extends HttpDefaultHeaders {
  @override
  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) {
    //http.request('http://www.google.com/');
    var h = http;
        // debugger didn't show http so I filed https://code.google.com/p/dart/issues/detail?id=17486

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

  }
}


class MyAppModule extends Module {
  MyAppModule() {

    type(MyComponent);

    type(HttpDefaultHeaders, implementedBy: MyDefaultHeaders);

    init.createParser(this);
  }
}

I couldn’t check httpto check the headers because the debugger didn’t show me the field, but as indicated in the comment when I apply headers.setHeadersto make the map inside MyComponent, I get my own header (this is what s httpdoes headers) I used DI 0.0.33.Angular 0.9.9

+2
source

, , http- . .

HttpDefaultHeaders, .

headers['Common'].addAll({'Authorization': 'Basic $auth', 'X-Testing': 'Testing'});

, .

. angular 1.1.1. , .

+2

Have you tried something like this:

class SiteIceiModule extends Module { 
  SiteIceiModule() {
    // ...
    factory(HttpDefaultHeaders, (inj) => inj.get(HttpDefaultHeaders)..setHeader({...}));
    // ...
  }
}
0
source

All Articles