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:
parent
1004b052ba
commit
dd78399fc1
|
@ -148,6 +148,10 @@ class RestIntegration implements IntegrationBase {
|
||||||
response.headers,
|
response.headers,
|
||||||
{ downloadImages: this.config.downloadImages }
|
{ downloadImages: this.config.downloadImages }
|
||||||
)
|
)
|
||||||
|
let contentLength = response.headers.get("content-length")
|
||||||
|
if (!contentLength && raw) {
|
||||||
|
contentLength = Buffer.byteLength(raw, "utf8").toString()
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
contentDisposition.includes("filename") ||
|
contentDisposition.includes("filename") ||
|
||||||
contentDisposition.includes("attachment") ||
|
contentDisposition.includes("attachment") ||
|
||||||
|
@ -156,36 +160,44 @@ class RestIntegration implements IntegrationBase {
|
||||||
filename =
|
filename =
|
||||||
path.basename(parse(contentDisposition).parameters?.filename) || ""
|
path.basename(parse(contentDisposition).parameters?.filename) || ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let triedParsing: boolean = false,
|
||||||
|
responseTxt: string | undefined
|
||||||
|
const hasContent = contentLength && parseInt(contentLength) > 0
|
||||||
try {
|
try {
|
||||||
if (filename) {
|
if (filename) {
|
||||||
return handleFileResponse(response, filename, this.startTimeMs)
|
return handleFileResponse(response, filename, this.startTimeMs)
|
||||||
} else {
|
} else {
|
||||||
|
responseTxt = hasContent ? await response.text() : ""
|
||||||
if (response.status === 204) {
|
if (response.status === 204) {
|
||||||
data = []
|
data = []
|
||||||
raw = ""
|
raw = ""
|
||||||
} else if (contentType.includes("application/json")) {
|
} else if (hasContent && contentType.includes("application/json")) {
|
||||||
data = await response.json()
|
triedParsing = true
|
||||||
raw = JSON.stringify(data)
|
data = JSON.parse(responseTxt)
|
||||||
|
raw = responseTxt
|
||||||
} else if (
|
} else if (
|
||||||
contentType.includes("text/xml") ||
|
(hasContent && contentType.includes("text/xml")) ||
|
||||||
contentType.includes("application/xml")
|
contentType.includes("application/xml")
|
||||||
) {
|
) {
|
||||||
let xmlResponse = await handleXml(response)
|
triedParsing = true
|
||||||
|
let xmlResponse = await handleXml(responseTxt)
|
||||||
data = xmlResponse.data
|
data = xmlResponse.data
|
||||||
raw = xmlResponse.rawXml
|
raw = xmlResponse.rawXml
|
||||||
} else {
|
} else {
|
||||||
data = await response.text()
|
data = responseTxt
|
||||||
raw = data as string
|
raw = data as string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} 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 size = formatBytes(contentLength || "0")
|
||||||
const time = `${Math.round(performance.now() - this.startTimeMs)}ms`
|
const time = `${Math.round(performance.now() - this.startTimeMs)}ms`
|
||||||
headers = response.headers.raw()
|
headers = response.headers.raw()
|
||||||
|
|
|
@ -485,9 +485,8 @@ export function isValidFilter(value: any) {
|
||||||
return value != null && value !== ""
|
return value != null && value !== ""
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleXml(response: any) {
|
export async function handleXml(rawXml: string) {
|
||||||
let data,
|
let data
|
||||||
rawXml = await response.text()
|
|
||||||
data =
|
data =
|
||||||
(await xmlParser(rawXml, {
|
(await xmlParser(rawXml, {
|
||||||
explicitArray: false,
|
explicitArray: false,
|
||||||
|
|
Loading…
Reference in New Issue