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

Commit

Permalink
feat(http): saving response/request body even if not json (#378)
Browse files Browse the repository at this point in the history
* feat(http): saving response/request body even if not json

* fix(http): leave the JSON treatment as before
  • Loading branch information
enoodle authored Oct 18, 2020
1 parent e9789d0 commit a0c6c2d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/helpers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,39 @@ function resolveHttpPromise(httpEvent, resolveFunction, startTime) {


/**
* Set the duration of the event, and resolves the promise using the given function.
* Attempts to json parse the data and set it at key on the event's metadata.
* @param {object} httpEvent The current event
* @param {string} key name in metadata
* @param {string} data data to jsonify
* @param {string} encoding data encoding from the headers
*/
function setJsonPayload(httpEvent, key, data, encoding) {
try {
let jsonData = data;
let decodedData = data;
if (config.getConfig().decodeHTTP && ENCODING_FUNCTIONS[encoding]) {
try {
jsonData = ENCODING_FUNCTIONS[encoding](data);
decodedData = ENCODING_FUNCTIONS[encoding](data);
} catch (err) {
utils.debugLog(`Could decode ${key} with ${encoding} in http`);
}
}
JSON.parse(jsonData);
const jsonData = decodedData;
try {
JSON.parse(jsonData);
eventInterface.addToMetadata(httpEvent, {}, {
[key]: jsonData.toString(),
});
} catch (err) {
utils.debugLog(`Could not parse JSON ${key} in http`);
eventInterface.addToMetadata(httpEvent, {}, {
[key]: decodedData.toString('utf-8'),
});
}
} catch (err) {
utils.debugLog(`Could not decode data and parse JSON ${key} in http`);
eventInterface.addToMetadata(httpEvent, {}, {
[key]: jsonData.toString(),
[key]: data,
});
} catch (err) {
utils.debugLog(`Could not parse JSON ${key} in http`);
}
}

Expand Down

0 comments on commit a0c6c2d

Please sign in to comment.