Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit a0c6c2d

Browse files
authored
feat(http): saving response/request body even if not json (#378)
* feat(http): saving response/request body even if not json * fix(http): leave the JSON treatment as before
1 parent e9789d0 commit a0c6c2d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/helpers/http.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,39 @@ function resolveHttpPromise(httpEvent, resolveFunction, startTime) {
5555

5656

5757
/**
58-
* Set the duration of the event, and resolves the promise using the given function.
58+
* Attempts to json parse the data and set it at key on the event's metadata.
5959
* @param {object} httpEvent The current event
6060
* @param {string} key name in metadata
6161
* @param {string} data data to jsonify
6262
* @param {string} encoding data encoding from the headers
6363
*/
6464
function setJsonPayload(httpEvent, key, data, encoding) {
6565
try {
66-
let jsonData = data;
66+
let decodedData = data;
6767
if (config.getConfig().decodeHTTP && ENCODING_FUNCTIONS[encoding]) {
6868
try {
69-
jsonData = ENCODING_FUNCTIONS[encoding](data);
69+
decodedData = ENCODING_FUNCTIONS[encoding](data);
7070
} catch (err) {
7171
utils.debugLog(`Could decode ${key} with ${encoding} in http`);
7272
}
7373
}
74-
JSON.parse(jsonData);
74+
const jsonData = decodedData;
75+
try {
76+
JSON.parse(jsonData);
77+
eventInterface.addToMetadata(httpEvent, {}, {
78+
[key]: jsonData.toString(),
79+
});
80+
} catch (err) {
81+
utils.debugLog(`Could not parse JSON ${key} in http`);
82+
eventInterface.addToMetadata(httpEvent, {}, {
83+
[key]: decodedData.toString('utf-8'),
84+
});
85+
}
86+
} catch (err) {
87+
utils.debugLog(`Could not decode data and parse JSON ${key} in http`);
7588
eventInterface.addToMetadata(httpEvent, {}, {
76-
[key]: jsonData.toString(),
89+
[key]: data,
7790
});
78-
} catch (err) {
79-
utils.debugLog(`Could not parse JSON ${key} in http`);
8091
}
8192
}
8293

0 commit comments

Comments
 (0)