From 5f99819a67c51b84e29cfb5c14c22627523f2488 Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Wed, 9 Jul 2025 16:58:51 +0530 Subject: [PATCH] fix: unicode error in postman import (#5213) --- .../helpers/import-export/import/postman.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 f396c4b6..2fa68820 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({