Skip to content

Commit

Permalink
fix: set method to POST when body exists (#14523) (#14524)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiakaic authored Feb 28, 2025
1 parent 55405c1 commit be18b10
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ const parseCurl = (curlCommand: string): { node: HttpNodeType | null; error: str
const node: Partial<HttpNodeType> = {
title: 'HTTP Request',
desc: 'Imported from cURL',
method: Method.get,
method: undefined,
url: '',
headers: '',
params: '',
body: { type: BodyType.none, data: '' },
}
const args = curlCommand.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || []
let hasData = false

for (let i = 1; i < args.length; i++) {
const arg = args[i].replace(/^['"]|['"]$/g, '')
Expand All @@ -38,6 +39,7 @@ const parseCurl = (curlCommand: string): { node: HttpNodeType | null; error: str
if (i + 1 >= args.length)
return { node: null, error: 'Missing HTTP method after -X or --request.' }
node.method = (args[++i].replace(/^['"]|['"]$/g, '') as Method) || Method.get
hasData = true
break
case '-H':
case '--header':
Expand Down Expand Up @@ -89,6 +91,9 @@ const parseCurl = (curlCommand: string): { node: HttpNodeType | null; error: str
}
}

// Determine final method
node.method = node.method || (hasData ? Method.post : Method.get)

if (!node.url)
return { node: null, error: 'Missing URL or url not start with http.' }

Expand Down

0 comments on commit be18b10

Please sign in to comment.