Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c26b1b5
Draft
DigitalBrainJS Mar 18, 2021
4d06575
Merge branch 'master' of https://github.com/axios/axios into feat/jso…
DigitalBrainJS Mar 18, 2021
bc402a7
Added support for primitive types to be converted to JSON if the requ…
DigitalBrainJS Mar 19, 2021
5112ba3
Fixed isOlderVersion helper;
DigitalBrainJS Mar 19, 2021
aa63605
Added forcedJSONParsing transitional option #2791
DigitalBrainJS Mar 19, 2021
b30a737
Merge branch 'master' into feat/json-improvements
DigitalBrainJS Mar 23, 2021
7cdfb90
Merge branch 'master' of https://github.com/axios/axios into feat/jso…
DigitalBrainJS Mar 24, 2021
ba6d2bc
`transformData` is now called in the default configuration context if…
DigitalBrainJS Mar 24, 2021
07441a6
Merge remote-tracking branch 'origin/feat/json-improvements' into fea…
DigitalBrainJS Mar 24, 2021
bb5b761
Merge branch 'master' into feat/json-improvements
DigitalBrainJS Mar 29, 2021
b8bd73c
Merge branch 'master' into feat/json-improvements
DigitalBrainJS Apr 2, 2021
e1340e0
Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error ins…
DigitalBrainJS Apr 5, 2021
f2dd788
Merge branch 'master' of https://github.com/axios/axios into feat/jso…
DigitalBrainJS Apr 5, 2021
d7f938b
Merge remote-tracking branch 'origin/feat/json-improvements' into fea…
DigitalBrainJS Apr 5, 2021
968a7b1
Removed unnecessary assertion;
DigitalBrainJS Apr 19, 2021
1d8ae08
Merge branch 'master' of https://github.com/axios/axios into feat/jso…
DigitalBrainJS Apr 19, 2021
e31cded
Merge branch 'master' of https://github.com/axios/axios into feat/jso…
DigitalBrainJS Sep 5, 2021
5ea2c95
Added safe JSON stringification to avoid unnecessary string encoding.
DigitalBrainJS Sep 5, 2021
21fb500
removed redundant parentheses.
DigitalBrainJS Sep 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ These are the available config options for making requests. Only the `url` is re
silentJSONParsing: true, // default value for the current Axios version

// try to parse the response string as JSON even if `responseType` is not 'json'
forcedJSONParsing: true;
forcedJSONParsing: true,

// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
clarifyTimeoutError: false,
clarifyTimeoutError: false
}
}
```
Expand Down
18 changes: 17 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ function getDefaultAdapter() {
return adapter;
}

function stringifySafely(rawValue, parser, encoder) {
if (utils.isString(rawValue)) {
try {
(parser || JSON.parse)(rawValue);
return utils.trim(rawValue);
} catch (e) {
if (e.name !== 'SyntaxError') {
throw e;
}
}
}

return (encoder || JSON.stringify)(rawValue);
}

var defaults = {

transitional: {
Expand Down Expand Up @@ -56,9 +71,10 @@ var defaults = {
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
return data.toString();
}

if (utils.isObject(data) || (headers && headers['Content-Type'] === 'application/json')) {
setContentTypeIfUnset(headers, 'application/json');
return JSON.stringify(data);
return stringifySafely(data);
}
return data;
}],
Expand Down
27 changes: 27 additions & 0 deletions test/specs/transform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,31 @@ describe('transform', function () {
done();
});
});

it('should not stringify twice if the payload was already encoded to the JSON string', function (done) {
var response;
var rawPayload = '{"x": 123}';

axios.post('/foo', rawPayload, {
responseType: 'text',
headers: {'Content-Type': 'application/json'}
}).then(function (_response) {
response = _response;
}, function (err) {
done(err);
});

getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: request.body
});

setTimeout(function () {
expect(response).toBeTruthy();
expect(request.params).toEqual(rawPayload);
done();
}, 100);
});
});
});