Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can't import curl command with data-urlencode #3905

Merged
merged 5 commits into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -868,6 +868,38 @@ const samples = [
requestVariables: [],
}),
},
{
command: `curl --location 'https://api.example.net/id/1164/requests' \
--header 'Accept: application/vnd.test-data.v2.1+json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'data={"type":"test","typeId":"101"}' \
--data-urlencode 'data2={"type":"test2","typeId":"123"}'`,
response: makeRESTRequest({
method: "POST",
name: "Untitled",
endpoint: "https://api.example.net/id/1164/requests",
auth: {
authType: "inherit",
authActive: true,
},
body: {
contentType: "application/x-www-form-urlencoded",
body: `data: {"type":"test","typeId":"101"}
data2: {"type":"test2","typeId":"123"}`,
},
params: [],
headers: [
{
active: true,
key: "Accept",
value: "application/vnd.test-data.v2.1+json",
},
],
preRequestScript: "",
testScript: "",
requestVariables: [],
}),
},
]

describe("Parse curl command to Hopp REST Request", () => {
Expand Down
22 changes: 21 additions & 1 deletion packages/hoppscotch-common/src/helpers/curl/curlparser.ts
Expand Up @@ -33,7 +33,27 @@ export const parseCurlCommand = (curlCommand: string) => {
// const compressed = !!parsedArguments.compressed

curlCommand = preProcessCurlCommand(curlCommand)
const parsedArguments = parser(curlCommand)

const args: parser.Arguments = parser(curlCommand)

const parsedArguments = pipe(
args,
O.fromPredicate(
(args) =>
objHasProperty("dataUrlencode", "string")(args) ||
objHasProperty("dataUrlencode", "object")(args)
),
O.map((args) => {
const urlEncodedData: string[] = Array.isArray(args.dataUrlencode)
? args.dataUrlencode
: [args.dataUrlencode]

const data = A.map(decodeURIComponent)(urlEncodedData)

return { ...args, d: data }
}),
O.getOrElse(() => args)
)

const headerObject = getHeaders(parsedArguments)
const { headers } = headerObject
Expand Down