@@ -220,7 +220,7 @@ function $HttpProvider() {
220
220
* it is important to familiarize yourself with these APIs and the guarantees they provide.
221
221
*
222
222
*
223
- * # General usage
223
+ * ## General usage
224
224
* The `$http` service is a function which takes a single argument — a configuration object —
225
225
* that is used to generate an HTTP request and returns a {@link ng.$q promise}
226
226
* with two $http specific methods: `success` and `error`.
@@ -247,7 +247,7 @@ function $HttpProvider() {
247
247
* XMLHttpRequest will transparently follow it, meaning that the error callback will not be
248
248
* called for such responses.
249
249
*
250
- * # Writing Unit Tests that use $http
250
+ * ## Writing Unit Tests that use $http
251
251
* When unit testing (using {@link ngMock ngMock}), it is necessary to call
252
252
* {@link ngMock.$httpBackend#flush $httpBackend.flush()} to flush each pending
253
253
* request using trained responses.
@@ -258,7 +258,7 @@ function $HttpProvider() {
258
258
* $httpBackend.flush();
259
259
* ```
260
260
*
261
- * # Shortcut methods
261
+ * ## Shortcut methods
262
262
*
263
263
* Shortcut methods are also available. All shortcut methods require passing in the URL, and
264
264
* request data must be passed in for POST/PUT requests.
@@ -279,7 +279,7 @@ function $HttpProvider() {
279
279
* - {@link ng.$http#patch $http.patch}
280
280
*
281
281
*
282
- * # Setting HTTP Headers
282
+ * ## Setting HTTP Headers
283
283
*
284
284
* The $http service will automatically add certain HTTP headers to all requests. These defaults
285
285
* can be fully configured by accessing the `$httpProvider.defaults.headers` configuration
@@ -310,36 +310,69 @@ function $HttpProvider() {
310
310
* calling `$http(config)`, which overrides the defaults without changing them globally.
311
311
*
312
312
*
313
- * # Transforming Requests and Responses
313
+ * ## Transforming Requests and Responses
314
314
*
315
- * Both requests and responses can be transformed using transform functions. By default, Angular
316
- * applies these transformations:
315
+ * Both requests and responses can be transformed using transformation functions: `transformRequest`
316
+ * and `transformResponse`. These properties can be a single function that returns
317
+ * the transformed value (`{function(data, headersGetter)`) or an array of such transformation functions,
318
+ * which allows you to `push` or `unshift` a new transformation function into the transformation chain.
317
319
*
318
- * Request transformations:
320
+ * ### Default Transformations
321
+ *
322
+ * The `$httpProvider` provider and `$http` service expose `defaults.transformRequest` and
323
+ * `defaults.transformResponse` properties. If a request does not provide its own transformations
324
+ * then these will be applied.
325
+ *
326
+ * You can augment or replace the default transformations by modifying these properties by adding to or
327
+ * replacing the array.
328
+ *
329
+ * Angular provides the following default transformations:
330
+ *
331
+ * Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`):
319
332
*
320
333
* - If the `data` property of the request configuration object contains an object, serialize it
321
334
* into JSON format.
322
335
*
323
- * Response transformations:
336
+ * Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`) :
324
337
*
325
338
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
326
339
* - If JSON response is detected, deserialize it using a JSON parser.
327
340
*
328
- * To globally augment or override the default transforms, modify the
329
- * `$httpProvider.defaults.transformRequest` and `$httpProvider.defaults.transformResponse`
330
- * properties. These properties are by default an array of transform functions, which allows you
331
- * to `push` or `unshift` a new transformation function into the transformation chain. You can
332
- * also decide to completely override any default transformations by assigning your
333
- * transformation functions to these properties directly without the array wrapper. These defaults
334
- * are again available on the $http factory at run-time, which may be useful if you have run-time
335
- * services you wish to be involved in your transformations.
336
- *
337
- * Similarly, to locally override the request/response transforms, augment the
338
- * `transformRequest` and/or `transformResponse` properties of the configuration object passed
341
+ *
342
+ * ### Overriding the Default Transformations Per Request
343
+ *
344
+ * If you wish override the request/response transformations only for a single request then provide
345
+ * `transformRequest` and/or `transformResponse` properties on the configuration object passed
339
346
* into `$http`.
340
347
*
348
+ * Note that if you provide these properties on the config object the default transformations will be
349
+ * overwritten. If you wish to augment the default transformations then you must include them in your
350
+ * local transformation array.
351
+ *
352
+ * The following code demonstrates adding a new response transformation to be run after the default response
353
+ * transformations have been run.
354
+ *
355
+ * ```js
356
+ * function appendTransform(defaults, transform) {
357
+ *
358
+ * // We can't guarantee that the default transformation is an array
359
+ * defaults = angular.isArray(defaults) ? defaults : [defaults];
360
+ *
361
+ * // Append the new transformation to the defaults
362
+ * return defaults.concat(transform);
363
+ * }
364
+ *
365
+ * $http({
366
+ * url: '...',
367
+ * method: 'GET',
368
+ * transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
369
+ * return doTransform(value);
370
+ * })
371
+ * });
372
+ * ```
373
+ *
341
374
*
342
- * # Caching
375
+ * ## Caching
343
376
*
344
377
* To enable caching, set the request configuration `cache` property to `true` (to use default
345
378
* cache) or to a custom cache object (built with {@link ng.$cacheFactory `$cacheFactory`}).
@@ -362,7 +395,7 @@ function $HttpProvider() {
362
395
* If you set the default cache to `false` then only requests that specify their own custom
363
396
* cache object will be cached.
364
397
*
365
- * # Interceptors
398
+ * ## Interceptors
366
399
*
367
400
* Before you start creating interceptors, be sure to understand the
368
401
* {@link ng.$q $q and deferred/promise APIs}.
@@ -447,7 +480,7 @@ function $HttpProvider() {
447
480
* });
448
481
* ```
449
482
*
450
- * # Security Considerations
483
+ * ## Security Considerations
451
484
*
452
485
* When designing web applications, consider security threats from:
453
486
*
@@ -458,7 +491,7 @@ function $HttpProvider() {
458
491
* pre-configured with strategies that address these issues, but for this to work backend server
459
492
* cooperation is required.
460
493
*
461
- * ## JSON Vulnerability Protection
494
+ * ### JSON Vulnerability Protection
462
495
*
463
496
* A [JSON vulnerability](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx)
464
497
* allows third party website to turn your JSON resource URL into
@@ -480,7 +513,7 @@ function $HttpProvider() {
480
513
* Angular will strip the prefix, before processing the JSON.
481
514
*
482
515
*
483
- * ## Cross Site Request Forgery (XSRF) Protection
516
+ * ### Cross Site Request Forgery (XSRF) Protection
484
517
*
485
518
* [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) is a technique by which
486
519
* an unauthorized site can gain your user's private data. Angular provides a mechanism
@@ -522,10 +555,12 @@ function $HttpProvider() {
522
555
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
523
556
* transform function or an array of such functions. The transform function takes the http
524
557
* request body and headers and returns its transformed (typically serialized) version.
558
+ * See {@link #overriding-the-default-transformations-per-request Overriding the Default Transformations}
525
559
* - **transformResponse** –
526
560
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
527
561
* transform function or an array of such functions. The transform function takes the http
528
562
* response body and headers and returns its transformed (typically deserialized) version.
563
+ * See {@link #overriding-the-default-transformations-per-request Overriding the Default Transformations}
529
564
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
530
565
* GET request, otherwise if a cache instance built with
531
566
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
0 commit comments