feat: correctly format json when importing from curl (#1472)

This commit is contained in:
Anton Ödman
2024-01-29 18:59:31 +01:00
committed by GitHub
parent d999366a16
commit 467e63d6fa
4 changed files with 83 additions and 41 deletions

View File

@@ -39,9 +39,10 @@
"idb": "^7.0.0",
"immer": "^9.0.15",
"jsesc": "^3.0.2",
"jsonpath-plus": "^7.2.0",
"jshint": "^2.13.6",
"json5": "^2.2.3",
"jsonlint": "^1.6.3",
"jsonpath-plus": "^7.2.0",
"know-your-http-well": "^0.5.0",
"lodash": "^4.17.21",
"markdown-it": "^13.0.2",

View File

@@ -51,6 +51,14 @@ export const safeStringifyJSON = (obj, indent = false) => {
}
};
export const convertToCodeMirrorJson = (obj) => {
try {
return JSON5.stringify(obj).slice(1, -1);
} catch (e) {
return obj;
}
};
export const safeParseXML = (str, options) => {
if (!str || !str.length || typeof str !== 'string') {
return str;

View File

@@ -1,5 +1,5 @@
import { forOwn } from 'lodash';
import { safeStringifyJSON } from 'utils/common';
import { convertToCodeMirrorJson } from 'utils/common';
import curlToJson from './curl-to-json';
export const getRequestFromCurlCommand = (curlCommand) => {
@@ -37,7 +37,7 @@ export const getRequestFromCurlCommand = (curlCommand) => {
if (parsedBody && contentType && typeof contentType === 'string') {
if (contentType.includes('application/json')) {
body.mode = 'json';
body.json = safeStringifyJSON(parsedBody);
body.json = convertToCodeMirrorJson(parsedBody);
} else if (contentType.includes('text/xml')) {
body.mode = 'xml';
body.xml = parsedBody;