Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: unicode error in postman import (#5213)

Nivedin5f99819a67c5
diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts b/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
index f396c4b69..2fa688206 100644
--- a/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
+++ b/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
@@ -147,6 +147,25 @@ const getHoppReqVariables = (
}
}
+// This regex is used to remove unsupported unicode characters from the response body which fails in Prisma
+// https://dba.stackexchange.com/questions/115029/unicode-error-with-u0000-on-copy-of-large-json-file-into-postgres
+const UNSUPPORTED_UNICODES_REGEX = /[\u0000]/g
+
+const getHoppResponseBody = (
+ body: string | ArrayBuffer | undefined
+): string => {
+ if (!body) return ""
+ if (typeof body === "string")
+ return body.replace(UNSUPPORTED_UNICODES_REGEX, "")
+ if (body instanceof ArrayBuffer) {
+ return new TextDecoder()
+ .decode(body)
+ .replace(UNSUPPORTED_UNICODES_REGEX, "")
+ }
+
+ return ""
+}
+
const getHoppResponses = (
responses: Item["responses"]
): HoppRESTRequestResponses => {
@@ -157,7 +176,7 @@ const getHoppResponses = (
const res = {
name: response.name,
status: response.status,
- body: response.body ?? "",
+ body: getHoppResponseBody(response.body),
headers: getHoppReqHeaders(response.headers),
code: response.code,
originalRequest: makeHoppRESTResponseOriginalRequest({