Skip to content

Commit e23782b

Browse files
authored
docs($http): correct and clarify default transforms
- baddata error described incorrect http behavior, and workarounds - httpProvider defaults were missing transformResponse / transformRequest - http was not clear about JSON detection strategy Closes angular#15897 Closes angular#15906
1 parent d0622d0 commit e23782b

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

docs/content/error/$http/baddata.ngdoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
@fullName Bad JSON Data
44
@description
55

6-
The default @{link ng.$http#default-transformations `transformResponse`} will try to parse the
7-
response as JSON if the `Content-Type` header is `application/json` or the response looks like a
6+
The default {@link ng.$http#default-transformations `transformResponse`} will try to parse the
7+
response as JSON if the `Content-Type` header is `application/json`, or the response looks like a
88
valid JSON-stringified object or array.
99
This error occurs when that data is not a valid JSON object.
1010

11-
The error message should provide additional context such as the actual response.
12-
13-
To resolve this error, make sure you pass valid JSON data to `transformResponse` or use an
14-
appropriate `Content-Type` header for non-JSON data.
11+
To resolve this error, make sure you pass valid JSON data to `transformResponse`. If the response
12+
data looks like JSON, but has a different `Content-Type` header, you must
13+
{@link ng.$http#overriding-the-default-transformations-per-request implement your own response
14+
transformer on a per request basis}, or {@link ng.$http#default-transformations modify the default `$http` responseTransform}.

src/ng/http.js

+32-12
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,6 @@ function $HttpProvider() {
266266
* {@link ng.$cacheFactory `$cacheFactory`} to enable or disable caching of HTTP responses
267267
* by default. See {@link $http#caching $http Caching} for more information.
268268
*
269-
* - **`defaults.xsrfCookieName`** - {string} - Name of cookie containing the XSRF token.
270-
* Defaults value is `'XSRF-TOKEN'`.
271-
*
272-
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
273-
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
274-
*
275269
* - **`defaults.headers`** - {Object} - Default headers for all $http requests.
276270
* Refer to {@link ng.$http#setting-http-headers $http} for documentation on
277271
* setting default headers.
@@ -280,15 +274,38 @@ function $HttpProvider() {
280274
* - **`defaults.headers.put`**
281275
* - **`defaults.headers.patch`**
282276
*
277+
* - **`defaults.jsonpCallbackParam`** - `{string}` - the name of the query parameter that passes the name of the
278+
* callback in a JSONP request. The value of this parameter will be replaced with the expression generated by the
279+
* {@link $jsonpCallbacks} service. Defaults to `'callback'`.
283280
*
284281
* - **`defaults.paramSerializer`** - `{string|function(Object<string,string>):string}` - A function
285282
* used to the prepare string representation of request parameters (specified as an object).
286283
* If specified as string, it is interpreted as a function registered with the {@link auto.$injector $injector}.
287284
* Defaults to {@link ng.$httpParamSerializer $httpParamSerializer}.
288285
*
289-
* - **`defaults.jsonpCallbackParam`** - `{string}` - the name of the query parameter that passes the name of the
290-
* callback in a JSONP request. The value of this parameter will be replaced with the expression generated by the
291-
* {@link $jsonpCallbacks} service. Defaults to `'callback'`.
286+
* - **`defaults.transformRequest`** -
287+
* `{Array<function(data, headersGetter)>|function(data, headersGetter)}` -
288+
* An array of functions (or a single function) which are applied to the request data.
289+
* By default, this is an array with one request transformation function:
290+
*
291+
* - If the `data` property of the request configuration object contains an object, serialize it
292+
* into JSON format.
293+
*
294+
* - **`defaults.transformResponse`** -
295+
* `{Array<function(data, headersGetter, status)>|function(data, headersGetter, status)}` -
296+
* An array of functions (or a single function) which are applied to the response data. By default,
297+
* this is an array which applies one response transformation function that does two things:
298+
*
299+
* - If XSRF prefix is detected, strip it
300+
* (see {@link ng.$http#security-considerations Security Considerations in the $http docs}).
301+
* - If the `Content-Type` is `application/json` or the response looks like JSON,
302+
* deserialize it using a JSON parser.
303+
*
304+
* - **`defaults.xsrfCookieName`** - {string} - Name of cookie containing the XSRF token.
305+
* Defaults value is `'XSRF-TOKEN'`.
306+
*
307+
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
308+
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
292309
*
293310
**/
294311
var defaults = this.defaults = {
@@ -552,15 +569,18 @@ function $HttpProvider() {
552569
*
553570
* AngularJS provides the following default transformations:
554571
*
555-
* Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`):
572+
* Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`) is
573+
* an array with one function that does the following:
556574
*
557575
* - If the `data` property of the request configuration object contains an object, serialize it
558576
* into JSON format.
559577
*
560-
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`):
578+
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`) is
579+
* an array with one function that does the following:
561580
*
562581
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
563-
* - If JSON response is detected, deserialize it using a JSON parser.
582+
* - If the `Content-Type` is `application/json` or the response looks like JSON,
583+
* deserialize it using a JSON parser.
564584
*
565585
*
566586
* ### Overriding the Default Transformations Per Request

0 commit comments

Comments
 (0)