数据样式:
期待格式:
const handleCopyCURL = (requestData) => {
const lines = requestData.split('\n').map((line) => line.trim())
const requestLine = lines.shift()
const [method, url, protocolVersion] = requestLine.split(' ')
const headers = lines.filter((line) => !!line).map((line) => line.split(': '))
const headerMap = new Map(headers)
let curlCommand = 'curl \\\n'
curlCommand += ` -X ${method} \\\n`
for (const [headerName, headerValue] of headerMap) {
curlCommand += ` -H "${headerName}: ${headerValue}" \\\n`
}
curlCommand += ` ${url}`
console.log(curlCommand)
return curlCommand
}
//使用方式:
handleCopyCURL(data)