Skip to content

Commit 5f99819

Browse files
authored
fix: unicode error in postman import (hoppscotch#5213)
1 parent f97fecd commit 5f99819

File tree

1 file changed

+20
-1
lines changed
  • packages/hoppscotch-common/src/helpers/import-export/import

1 file changed

+20
-1
lines changed

packages/hoppscotch-common/src/helpers/import-export/import/postman.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ const getHoppReqVariables = (
147147
}
148148
}
149149

150+
// This regex is used to remove unsupported unicode characters from the response body which fails in Prisma
151+
// https://dba.stackexchange.com/questions/115029/unicode-error-with-u0000-on-copy-of-large-json-file-into-postgres
152+
const UNSUPPORTED_UNICODES_REGEX = /[\u0000]/g
153+
154+
const getHoppResponseBody = (
155+
body: string | ArrayBuffer | undefined
156+
): string => {
157+
if (!body) return ""
158+
if (typeof body === "string")
159+
return body.replace(UNSUPPORTED_UNICODES_REGEX, "")
160+
if (body instanceof ArrayBuffer) {
161+
return new TextDecoder()
162+
.decode(body)
163+
.replace(UNSUPPORTED_UNICODES_REGEX, "")
164+
}
165+
166+
return ""
167+
}
168+
150169
const getHoppResponses = (
151170
responses: Item["responses"]
152171
): HoppRESTRequestResponses => {
@@ -157,7 +176,7 @@ const getHoppResponses = (
157176
const res = {
158177
name: response.name,
159178
status: response.status,
160-
body: response.body ?? "",
179+
body: getHoppResponseBody(response.body),
161180
headers: getHoppReqHeaders(response.headers),
162181
code: response.code,
163182
originalRequest: makeHoppRESTResponseOriginalRequest({

0 commit comments

Comments
 (0)