diff --git a/packages/hoppscotch-app/components/collections/ImportExport.vue b/packages/hoppscotch-app/components/collections/ImportExport.vue index eeb8f9181..9f51f6ba3 100644 --- a/packages/hoppscotch-app/components/collections/ImportExport.vue +++ b/packages/hoppscotch-app/components/collections/ImportExport.vue @@ -277,6 +277,7 @@ const createCollectionGist = async () => { const fileImported = () => { toast.success(t("state.file_imported").toString()) + hideModal() } const failedImport = () => { @@ -403,6 +404,10 @@ const parsePostmanRequest = ({ headers: [] as { name?: string; type?: string }[], params: [] as { disabled?: boolean }[], bodyParams: [] as { type?: string }[], + body: { + body: "", + contentType: "application/json", + }, rawParams: "", rawInput: false, contentType: "", @@ -418,31 +423,40 @@ const parsePostmanRequest = ({ if (requestObjectUrl) { pwRequest.url = requestObjectUrl[1] pwRequest.path = requestObjectUrl[2] ? requestObjectUrl[2] : "" + } else { + pwRequest.url = request.url.raw } } + pwRequest.method = request.method const itemAuth = request.auth ? request.auth : "" const authType = itemAuth ? itemAuth.type : "" - if (authType === "basic") { - pwRequest.auth = "Basic Auth" - pwRequest.httpUser = - itemAuth.basic[0].key === "username" - ? itemAuth.basic[0].value - : itemAuth.basic[1].value - pwRequest.httpPassword = - itemAuth.basic[0].key === "password" - ? itemAuth.basic[0].value - : itemAuth.basic[1].value - } else if (authType === "oauth2") { - pwRequest.auth = "OAuth 2.0" - pwRequest.bearerToken = - itemAuth.oauth2[0].key === "accessToken" - ? itemAuth.oauth2[0].value - : itemAuth.oauth2[1].value - } else if (authType === "bearer") { - pwRequest.auth = "Bearer Token" - pwRequest.bearerToken = itemAuth.bearer[0].value + + try { + if (authType === "basic") { + pwRequest.auth = "Basic Auth" + pwRequest.httpUser = + itemAuth.basic[0].key === "username" + ? itemAuth.basic[0].value + : itemAuth.basic[1].value + pwRequest.httpPassword = + itemAuth.basic[0].key === "password" + ? itemAuth.basic[0].value + : itemAuth.basic[1].value + } else if (authType === "oauth2") { + pwRequest.auth = "OAuth 2.0" + pwRequest.bearerToken = + itemAuth.oauth2[0].key === "accessToken" + ? itemAuth.oauth2[0].value + : itemAuth.oauth2[1].value + } else if (authType === "bearer") { + pwRequest.auth = "Bearer Token" + pwRequest.bearerToken = itemAuth.bearer[0].value + } + } catch (error) { + console.error(error) } + const requestObjectHeaders = request.header if (requestObjectHeaders) { pwRequest.headers = requestObjectHeaders @@ -470,6 +484,12 @@ const parsePostmanRequest = ({ } else if (request.body.mode === "raw") { pwRequest.rawInput = true pwRequest.rawParams = request.body.raw + try { + const body = JSON.parse(request.body.raw) + pwRequest.body.body = JSON.stringify(body, null, 2) + } catch (error) { + console.error(error) + } } } return translateToNewRequest(pwRequest) diff --git a/packages/hoppscotch-app/components/http/ImportCurl.vue b/packages/hoppscotch-app/components/http/ImportCurl.vue index 2f922cc1a..e9a38c85e 100644 --- a/packages/hoppscotch-app/components/http/ImportCurl.vue +++ b/packages/hoppscotch-app/components/http/ImportCurl.vue @@ -84,6 +84,7 @@ const handleImport = () => { const endpoint = origin + pathname const headers: HoppRESTHeader[] = [] const params: HoppRESTParam[] = [] + const body = parsedCurl.body if (parsedCurl.query) { for (const key of Object.keys(parsedCurl.query)) { const val = parsedCurl.query[key]! @@ -114,6 +115,7 @@ const handleImport = () => { }) } } + const method = parsedCurl.method.toUpperCase() setRESTRequest( @@ -131,7 +133,7 @@ const handleImport = () => { }, body: { contentType: "application/json", - body: "", + body, }, }) ) diff --git a/packages/hoppscotch-app/helpers/curlparser.ts b/packages/hoppscotch-app/helpers/curlparser.ts index 1892049b0..e15bdbd03 100644 --- a/packages/hoppscotch-app/helpers/curlparser.ts +++ b/packages/hoppscotch-app/helpers/curlparser.ts @@ -73,6 +73,7 @@ const parseCurlCommand = (curlCommand: string) => { curlCommand = curlCommand.replace(/--request /, "-X ") curlCommand = curlCommand.replace(/--header /, "-H ") curlCommand = curlCommand.replace(/--url /, " ") + curlCommand = curlCommand.replace(/-d /, "--data ") // yargs parses -XPOST as separate arguments. just prescreen for it. curlCommand = curlCommand.replace(/ -XPOST/, " -X POST") @@ -82,6 +83,12 @@ const parseCurlCommand = (curlCommand: string) => { curlCommand = curlCommand.replace(/ -XDELETE/, " -X DELETE") curlCommand = curlCommand.trim() const parsedArguments = parser(curlCommand) + + const rawData = + parsedArguments.data || + parsedArguments.dataRaw || + parsedArguments["data-raw"] + let cookieString let cookies let url = parsedArguments._[1] @@ -187,6 +194,21 @@ const parseCurlCommand = (curlCommand: string) => { method = "get" } + let body = "" + + if (rawData) { + try { + const tempBody = JSON.parse(rawData) + if (tempBody) { + body = JSON.stringify(tempBody, null, 2) + } + } catch (e) { + console.error( + "Error parsing JSON data. Please ensure that the data is valid JSON." + ) + } + } + const compressed = !!parsedArguments.compressed let urlObject = URL.parse(url) // eslint-disable-line @@ -227,6 +249,7 @@ const parseCurlCommand = (curlCommand: string) => { query, headers, method, + body, cookies, cookieString: cookieString?.replace("Cookie: ", ""), multipartUploads,