@@ -659,22 +659,28 @@ a#override-default-request-options
659
659
The `HttpModule` provides these default options via the `RequestOptions` token.
660
660
661
661
You can override these defaults to suit your application needs.
662
- The API docs suggest creating a custom sub-class of `RequestOptions`
663
- and providing that as a replacement class for the `RequestOptions` token .
662
+ by creating a custom sub-class of `RequestOptions`
663
+ that sets the default options for the application .
664
664
665
- Perhaps an easier way is to inject the current `RequestOptions` instance
666
- into an application module constructor and re-set its values directly.
667
- This sample sets the default `Content-Type` header to JSON in the root `AppModule` constructor:
668
- + makeExample('server-communication/ts/app/app.module.ts' , 'override-default-request-options' , 'app/app.module.ts (default request header override)' )( format ="." )
665
+ This sample creates a class that sets the default `Content-Type` header to JSON.
666
+ It exports a constant with the necessary `RequestOptions` provider to simplify registration in `AppModule`.
667
+
668
+ + makeExample('server-communication/ts/app/default-request-options.service.ts' , '' , 'app/default-request-options.service.ts' )( format ="." )
669
+ :marked
670
+ Then it registers the provider in the root `AppModule`.
671
+ + makeExample('server-communication/ts/app/app.module.ts' , 'provide-default-request-options' , 'app/app.module.ts (provide default request header)' )( format ="." )
672
+ .l-sub-section
673
+ :marked
674
+ Remember to include this provider during setup when unit testing the app's HTTP services.
669
675
:marked
670
676
After this change, the `header` option setting in `HeroService.addHero` is no longer necessary,
671
677
672
678
+ makeExample('server-communication/ts/app/toh/hero.service.ts' , 'addhero' , 'app/toh/hero.service.ts (addHero)' )( format ="." )
673
679
:marked
674
- You can confirm this works by looking at the network tab.
680
+ You can confirm that `DefaultRequestOptions` is working by examing HTTP requests in the browser developer tools' network tab.
675
681
If you're short-circuiting the server call with something like the [_in-memory web api_](#in-mem-web-api),
676
682
try commenting-out the `addHero` header option,
677
- set a breakpoint on the POST call, and step through to the backend request
683
+ set a breakpoint on the POST call, and step through the request processing
678
684
to verify the header is there.
679
685
680
686
Individual requests options, like this one, take precedence over the default `RequestOptions`.
0 commit comments