1
0
Fork 0
mirror of synced 2024-10-06 04:54:52 +13:00

Changing how content processing works for responses from REST API - try and reduce the chance of an error in the case of malformed data being returned.

This commit is contained in:
mike12345567 2024-05-23 17:56:48 +01:00
parent 1004b052ba
commit dd78399fc1
2 changed files with 25 additions and 14 deletions

View file

@ -148,6 +148,10 @@ class RestIntegration implements IntegrationBase {
response.headers,
{ downloadImages: this.config.downloadImages }
)
let contentLength = response.headers.get("content-length")
if (!contentLength && raw) {
contentLength = Buffer.byteLength(raw, "utf8").toString()
}
if (
contentDisposition.includes("filename") ||
contentDisposition.includes("attachment") ||
@ -156,36 +160,44 @@ class RestIntegration implements IntegrationBase {
filename =
path.basename(parse(contentDisposition).parameters?.filename) || ""
}
let triedParsing: boolean = false,
responseTxt: string | undefined
const hasContent = contentLength && parseInt(contentLength) > 0
try {
if (filename) {
return handleFileResponse(response, filename, this.startTimeMs)
} else {
responseTxt = hasContent ? await response.text() : ""
if (response.status === 204) {
data = []
raw = ""
} else if (contentType.includes("application/json")) {
data = await response.json()
raw = JSON.stringify(data)
} else if (hasContent && contentType.includes("application/json")) {
triedParsing = true
data = JSON.parse(responseTxt)
raw = responseTxt
} else if (
contentType.includes("text/xml") ||
(hasContent && contentType.includes("text/xml")) ||
contentType.includes("application/xml")
) {
let xmlResponse = await handleXml(response)
triedParsing = true
let xmlResponse = await handleXml(responseTxt)
data = xmlResponse.data
raw = xmlResponse.rawXml
} else {
data = await response.text()
data = responseTxt
raw = data as string
}
}
} catch (err) {
throw `Failed to parse response body: ${err}`
if (triedParsing) {
data = responseTxt
raw = data as string
} else {
throw new Error(`Failed to parse response body: ${err}`)
}
}
let contentLength = response.headers.get("content-length")
if (!contentLength && raw) {
contentLength = Buffer.byteLength(raw, "utf8").toString()
}
const size = formatBytes(contentLength || "0")
const time = `${Math.round(performance.now() - this.startTimeMs)}ms`
headers = response.headers.raw()

View file

@ -485,9 +485,8 @@ export function isValidFilter(value: any) {
return value != null && value !== ""
}
export async function handleXml(response: any) {
let data,
rawXml = await response.text()
export async function handleXml(rawXml: string) {
let data
data =
(await xmlParser(rawXml, {
explicitArray: false,