forked from axios/axios
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON improvements: throw if JSON parsing failed; number, boolean can …
…be passed directly as payload for encoding to JSON axios#2613, axios#61, axios#907 (axios#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option axios#2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
- Loading branch information
1 parent
d99d5fa
commit 5ad6994
Showing
14 changed files
with
403 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
var utils = require('./utils'); | ||
var normalizeHeaderName = require('./helpers/normalizeHeaderName'); | ||
var enhanceError = require('./core/enhanceError'); | ||
|
||
var DEFAULT_CONTENT_TYPE = { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
|
@@ -26,11 +27,19 @@ function getDefaultAdapter() { | |
} | ||
|
||
var defaults = { | ||
|
||
transitional: { | ||
silentJSONParsing: true, | ||
forcedJSONParsing: true, | ||
clarifyTimeoutError: false | ||
}, | ||
|
||
adapter: getDefaultAdapter(), | ||
|
||
transformRequest: [function transformRequest(data, headers) { | ||
normalizeHeaderName(headers, 'Accept'); | ||
normalizeHeaderName(headers, 'Content-Type'); | ||
|
||
if (utils.isFormData(data) || | ||
utils.isArrayBuffer(data) || | ||
utils.isBuffer(data) || | ||
|
@@ -47,21 +56,33 @@ var defaults = { | |
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8'); | ||
return data.toString(); | ||
} | ||
if (utils.isObject(data)) { | ||
if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
timemachine3030
Owner
|
||
setContentTypeIfUnset(headers, 'application/json;charset=utf-8'); | ||
return JSON.stringify(data); | ||
} | ||
return data; | ||
}], | ||
|
||
transformResponse: [function transformResponse(data) { | ||
var result = data; | ||
if (utils.isString(result) && result.length) { | ||
var transitional = this.transitional; | ||
var silentJSONParsing = transitional && transitional.silentJSONParsing; | ||
var forcedJSONParsing = transitional && transitional.forcedJSONParsing; | ||
var strictJSONParsing = !silentJSONParsing && this.responseType === 'json'; | ||
|
||
if (strictJSONParsing || (forcedJSONParsing && utils.isString(data) && data.length)) { | ||
try { | ||
result = JSON.parse(result); | ||
} catch (e) { /* Ignore */ } | ||
return JSON.parse(data); | ||
} catch (e) { | ||
if (strictJSONParsing) { | ||
if (e.name === 'SyntaxError') { | ||
throw enhanceError(e, this, 'E_JSON_PARSE'); | ||
} | ||
throw e; | ||
} | ||
} | ||
} | ||
return result; | ||
|
||
return data; | ||
}], | ||
|
||
/** | ||
|
Oops, something went wrong.
@timemachine3030 this is causing a bug where GET requests which have content-type: application/json add body null because now it's being parsed. This was not the case for axios 0.21.1 and is causing failures on such requests