Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 9a3cbd6

Browse files
authored
docs(http): show how to override default RequestOptions like headers (#2978)
1 parent 05cdd9a commit 9a3cbd6

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

public/docs/_examples/server-communication/ts/app/app.module.1.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { AppComponent } from './app.component';
1616
declarations: [ AppComponent ],
1717
bootstrap: [ AppComponent ]
1818
})
19-
export class AppModule { }
19+
export class AppModule {
20+
}
2021

2122

2223

public/docs/_examples/server-communication/ts/app/app.module.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
// #docplaster
12
// #docregion
23
import { NgModule } from '@angular/core';
34
import { BrowserModule } from '@angular/platform-browser';
45
import { FormsModule } from '@angular/forms';
5-
import { HttpModule, JsonpModule } from '@angular/http';
6+
// #docregion override-default-options
7+
import { HttpModule, JsonpModule, RequestOptions } from '@angular/http';
68

9+
// #enddocregion override-default-options
710
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
811
import { HeroData } from './hero-data';
912

@@ -34,8 +37,14 @@ import { WikiSmartComponent } from './wiki/wiki-smart.component';
3437
],
3538
bootstrap: [ AppComponent ]
3639
})
37-
export class AppModule { }
38-
40+
// #docregion override-default-request-options
41+
export class AppModule {
42+
constructor(requestOptions: RequestOptions) {
43+
// Set the default 'Content-Type' header
44+
requestOptions.headers.set('Content-Type', 'application/json');
45+
}
46+
}
47+
// #enddocregion override-default-request-options
3948

4049

4150

public/docs/ts/latest/guide/change-log.jade

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ block includes
55
The Angular documentation is a living document with continuous improvements.
66
This log calls attention to recent significant changes.
77

8+
## Http: how to set default request headers (and other request options) (2016-12-14)
9+
Added section on how to set default request headers (and other request options) to
10+
[Http](server-communication.html#override-default-request-options) guide.
11+
812
## Testing: added component test plunkers (2016-12-02)
913
Added two plunkers that each test _one simple component_ so you can write a component test plunker of your own: <live-example name="setup" plnkr="quickstart-specs">one</live-example> for the QuickStart seed's `AppComponent` and <live-example name="testing" plnkr="banner-specs">another</live-example> for the Testing guide's `BannerComponent`.
1014
Linked to these plunkers in [Testing](testing.html#live-examples) and [Setup anatomy](setup-systemjs-anatomy) guides.

public/docs/ts/latest/guide/server-communication.jade

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ block includes
3333
<li> [More fun with observables](#more-observables).</li>
3434
</ul>
3535
- [Guarding against Cross-Site Request Forgery](#xsrf).
36+
- [Override default request headers (and other request options)](#override-default-request-options).
3637
- [Appendix: Tour of Heroes _in-memory web api_](#in-mem-web-api).
3738

3839
A <live-example>live example</live-example> illustrates these topics.
@@ -647,10 +648,42 @@ a#xsrf
647648

648649
See the [XSRF topic on the Security page](security.html#xsrf) for more information about XSRF and Angular's `XSRFStrategy` counter measures.
649650

651+
a#override-default-request-options
652+
.l-main-section
653+
:marked
654+
## Override default request headers (and other request options)
655+
656+
Request options (such as headers) are merged into the
657+
[default _RequestOptions_](https://angular.io/docs/ts/latest/api/http/index/BaseRequestOptions-class.html "API: BaseRequestOptions")
658+
before the request is processed.
659+
The `HttpModule` provides these default options via the `RequestOptions` token.
660+
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.
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=".")
669+
:marked
670+
After this change, the `header` option setting in `HeroService.addHero` is no longer necessary,
671+
672+
+makeExample('server-communication/ts/app/toh/hero.service.ts', 'addhero', 'app/toh/hero.service.ts (addHero)')(format=".")
673+
:marked
674+
You can confirm this works by looking at the network tab.
675+
If you're short-circuiting the server call with something like the [_in-memory web api_](#in-mem-web-api),
676+
try commenting-out the `addHero` header option,
677+
set a breakpoint on the POST call, and step through to the backend request
678+
to verify the header is there.
679+
680+
Individual requests options, like this one, take precedence over the default `RequestOptions`.
681+
It might be wise to keep the `addHero` request header setting for extra safety.
682+
650683
a#in-mem-web-api
651684
.l-main-section
652685
:marked
653-
## Appendix: Tour of Heroes in-memory server
686+
## Appendix: Tour of Heroes _in-memory web api_
654687

655688
If the app only needed to retrieve data, you could get the heroes from a `heroes.json` file:
656689
- var _heroesJsonPath = (_docsFor == 'dart' ? 'web' : 'app') + '/heroes.json';

0 commit comments

Comments
 (0)