Files
hoppscotch/packages/hoppscotch-app/helpers/codegen/generators/curl.js
Deepanshu Dhruw 520ac8ede5 fix: code generators (#1985)
Co-authored-by: liyasthomas <liyascthomas@gmail.com>
2021-11-30 07:46:45 +05:30

46 lines
1.2 KiB
JavaScript

export const CurlCodegen = {
id: "curl",
name: "cURL",
language: "sh",
generator: ({
url,
pathName,
queryString,
auth,
httpUser,
httpPassword,
bearerToken,
method,
rawInput,
rawParams,
rawRequestBody,
headers,
}) => {
const requestString = []
requestString.push(`curl -X ${method}`)
requestString.push(` '${url}${pathName}?${queryString}'`)
if (auth === "Basic Auth") {
const basic = `${httpUser}:${httpPassword}`
requestString.push(
` -H 'Authorization: Basic ${window.btoa(
unescape(encodeURIComponent(basic))
)}'`
)
} else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
requestString.push(` -H 'Authorization: Bearer ${bearerToken}'`)
}
if (headers) {
headers.forEach(({ key, value }) => {
if (key) requestString.push(` -H '${key}: ${value}'`)
})
}
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
let requestBody = rawInput ? rawParams : rawRequestBody
requestBody = requestBody || ""
requestString.push(` -d '${requestBody}'`)
}
return requestString.join(" \\\n")
},
}