mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-29 07:34:07 +00:00
fix: improve handling of Buffer responses and set default charset to utf-8
This commit is contained in:
@@ -384,8 +384,8 @@ const parseDataFromResponse = (response, disableParsingResponseJson = false) =>
|
||||
// Parse the charset from content type: https://stackoverflow.com/a/33192813
|
||||
const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || '');
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals
|
||||
const charsetValue = charsetMatch?.[1];
|
||||
const dataBuffer = Buffer.from(response.data);
|
||||
const charsetValue = charsetMatch?.[1] || 'utf-8';
|
||||
const dataBuffer = Buffer.isBuffer(response.data) ? response.data : Buffer.from(response.data);
|
||||
// Overwrite the original data for backwards compatibility
|
||||
let data;
|
||||
if (iconv.encodingExists(charsetValue)) {
|
||||
@@ -407,6 +407,23 @@ const parseDataFromResponse = (response, disableParsingResponseJson = false) =>
|
||||
console.log('Failed to parse response data as JSON');
|
||||
}
|
||||
|
||||
// Handle Buffer responses that contain JSON
|
||||
if (Buffer.isBuffer(response.data)) {
|
||||
try {
|
||||
const decodedString = response.data.toString('utf-8');
|
||||
const parsedData = JSON.parse(decodedString);
|
||||
|
||||
if (parsedData && parsedData.type === "Buffer" && Array.isArray(parsedData.data)) {
|
||||
data = Buffer.from(parsedData.data).toString('utf-8');
|
||||
data = JSON.parse(data);
|
||||
} else {
|
||||
data = parsedData;
|
||||
}
|
||||
} catch {
|
||||
console.error('Failed to parse Buffer data as JSON');
|
||||
}
|
||||
}
|
||||
|
||||
return { data, dataBuffer };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user