Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 86c6be8

Browse files
docs($http): clarify overriding of default transformations
Closes #8590
1 parent ae952fb commit 86c6be8

File tree

1 file changed

+60
-25
lines changed

1 file changed

+60
-25
lines changed

src/ng/http.js

+60-25
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function $HttpProvider() {
220220
* it is important to familiarize yourself with these APIs and the guarantees they provide.
221221
*
222222
*
223-
* # General usage
223+
* ## General usage
224224
* The `$http` service is a function which takes a single argument — a configuration object —
225225
* that is used to generate an HTTP request and returns a {@link ng.$q promise}
226226
* with two $http specific methods: `success` and `error`.
@@ -247,7 +247,7 @@ function $HttpProvider() {
247247
* XMLHttpRequest will transparently follow it, meaning that the error callback will not be
248248
* called for such responses.
249249
*
250-
* # Writing Unit Tests that use $http
250+
* ## Writing Unit Tests that use $http
251251
* When unit testing (using {@link ngMock ngMock}), it is necessary to call
252252
* {@link ngMock.$httpBackend#flush $httpBackend.flush()} to flush each pending
253253
* request using trained responses.
@@ -258,7 +258,7 @@ function $HttpProvider() {
258258
* $httpBackend.flush();
259259
* ```
260260
*
261-
* # Shortcut methods
261+
* ## Shortcut methods
262262
*
263263
* Shortcut methods are also available. All shortcut methods require passing in the URL, and
264264
* request data must be passed in for POST/PUT requests.
@@ -279,7 +279,7 @@ function $HttpProvider() {
279279
* - {@link ng.$http#patch $http.patch}
280280
*
281281
*
282-
* # Setting HTTP Headers
282+
* ## Setting HTTP Headers
283283
*
284284
* The $http service will automatically add certain HTTP headers to all requests. These defaults
285285
* can be fully configured by accessing the `$httpProvider.defaults.headers` configuration
@@ -310,36 +310,69 @@ function $HttpProvider() {
310310
* calling `$http(config)`, which overrides the defaults without changing them globally.
311311
*
312312
*
313-
* # Transforming Requests and Responses
313+
* ## Transforming Requests and Responses
314314
*
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.
317319
*
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`):
319332
*
320333
* - If the `data` property of the request configuration object contains an object, serialize it
321334
* into JSON format.
322335
*
323-
* Response transformations:
336+
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`):
324337
*
325338
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
326339
* - If JSON response is detected, deserialize it using a JSON parser.
327340
*
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
339346
* into `$http`.
340347
*
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+
*
341374
*
342-
* # Caching
375+
* ## Caching
343376
*
344377
* To enable caching, set the request configuration `cache` property to `true` (to use default
345378
* cache) or to a custom cache object (built with {@link ng.$cacheFactory `$cacheFactory`}).
@@ -362,7 +395,7 @@ function $HttpProvider() {
362395
* If you set the default cache to `false` then only requests that specify their own custom
363396
* cache object will be cached.
364397
*
365-
* # Interceptors
398+
* ## Interceptors
366399
*
367400
* Before you start creating interceptors, be sure to understand the
368401
* {@link ng.$q $q and deferred/promise APIs}.
@@ -447,7 +480,7 @@ function $HttpProvider() {
447480
* });
448481
* ```
449482
*
450-
* # Security Considerations
483+
* ## Security Considerations
451484
*
452485
* When designing web applications, consider security threats from:
453486
*
@@ -458,7 +491,7 @@ function $HttpProvider() {
458491
* pre-configured with strategies that address these issues, but for this to work backend server
459492
* cooperation is required.
460493
*
461-
* ## JSON Vulnerability Protection
494+
* ### JSON Vulnerability Protection
462495
*
463496
* A [JSON vulnerability](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx)
464497
* allows third party website to turn your JSON resource URL into
@@ -480,7 +513,7 @@ function $HttpProvider() {
480513
* Angular will strip the prefix, before processing the JSON.
481514
*
482515
*
483-
* ## Cross Site Request Forgery (XSRF) Protection
516+
* ### Cross Site Request Forgery (XSRF) Protection
484517
*
485518
* [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) is a technique by which
486519
* an unauthorized site can gain your user's private data. Angular provides a mechanism
@@ -522,10 +555,12 @@ function $HttpProvider() {
522555
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
523556
* transform function or an array of such functions. The transform function takes the http
524557
* request body and headers and returns its transformed (typically serialized) version.
558+
* See {@link #overriding-the-default-transformations-per-request Overriding the Default Transformations}
525559
* - **transformResponse** –
526560
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
527561
* transform function or an array of such functions. The transform function takes the http
528562
* response body and headers and returns its transformed (typically deserialized) version.
563+
* See {@link #overriding-the-default-transformations-per-request Overriding the Default Transformations}
529564
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
530565
* GET request, otherwise if a cache instance built with
531566
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for

0 commit comments

Comments
 (0)