3

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());
  GlobalHttpHeaders.setup(inj);

But that don't work.

3 Answers 3

2

(I think) I got it working 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 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

Sign up to request clarification or add additional context in comments.

1 Comment

The above example was not working in Angular v12 because it needed the @Injectable() on the MyDefaultHeaders.
2

I'm a little late to the discussion, but the answer provided was not usable for me, as my http requests are made by a 3rd party library. But I figured out a way to change the default headers.

You can access and modify the HttpDefaultHeaders object like a map.

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

This also works with 3rd Party libraries like hammock.

Note: I used angular 1.1.1 I don't know in which version this was added.

Comments

0

Have you tried something like this:

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

2 Comments

Yes I have. It seems setHeader is not to set the headers of the HttpDefaultHeaders class but is called by HttpRequest (or how the Angular specific class is named) to get the headers in HttpDefaultHeaders set to itself. There is no way to modify the default headers configured in HttpDefaultHeaders. I tried to make DI to inject a custom class that implements that interface but DI seems to prevent the type configured by Angular to be overridden by AppModule. It is of cource possible that I made some mistake, but I invested quite some time.
This example results in Illegal argument(s): Cannot resolve a circular dependency! (resolving BlockCache -> Http -> HttpDefaults -> HttpDefaultHeaders -> HttpDefaultHeaders)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.