From 2541f7ed596b8fa944578753c94159faa839521d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=ADsli=20Konr=C3=A1=C3=B0=20Bj=C3=B6rnsson?= Date: Tue, 27 Aug 2024 02:39:19 +0000 Subject: [PATCH] Adds ability to force json or xml parsing (#1200) --- src/utils/requestVariableCacheValueProcessor.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/utils/requestVariableCacheValueProcessor.ts b/src/utils/requestVariableCacheValueProcessor.ts index 0c1a08e8..26fe3a6b 100644 --- a/src/utils/requestVariableCacheValueProcessor.ts +++ b/src/utils/requestVariableCacheValueProcessor.ts @@ -42,6 +42,8 @@ export class RequestVariableCacheValueProcessor { private static resolveHttpPart(http: HttpRequest | HttpResponse, httpPart: HttpPart, nameOrPath?: string): ResolveResult { if (httpPart === "body") { + const forceJsonExtension = 'asJson.'; + const forceXmlExtension = 'asXml.'; const { body, headers } = http; if (!body) { const message = http instanceof HttpRequest ? ResolveWarningMessage.RequestBodyNotExist : ResolveWarningMessage.ResponseBodyNotExist; @@ -57,12 +59,23 @@ export class RequestVariableCacheValueProcessor { return { state: ResolveState.Success, value: body }; } + let forceJson = false; + let forceXml = false; + if (nameOrPath.startsWith(forceJsonExtension)) { + nameOrPath = nameOrPath.substring(forceJsonExtension.length); + forceJson = true; + } else if (nameOrPath.startsWith(forceXmlExtension)) { + nameOrPath = nameOrPath.substring(forceXmlExtension.length); + forceXml = true; + } + const contentTypeHeader = getContentType(headers); - if (MimeUtility.isJSON(contentTypeHeader) || (MimeUtility.isJavaScript(contentTypeHeader) && isJSONString(body as string))) { + if (MimeUtility.isJSON(contentTypeHeader) || + (forceJson || MimeUtility.isJavaScript(contentTypeHeader)) && isJSONString(body as string)) { const parsedBody = JSON.parse(body as string); return this.resolveJsonHttpBody(parsedBody, nameOrPath); - } else if (MimeUtility.isXml(contentTypeHeader)) { + } else if (forceXml || MimeUtility.isXml(contentTypeHeader)) { return this.resolveXmlHttpBody(body, nameOrPath); } else { return { state: ResolveState.Warning, value: body, message: ResolveWarningMessage.UnsupportedBodyContentType };