Files
hoppscotch/helpers/codegen/generators/curl.js
YE Qing 1afd0381c1 Run tests against all codegen instead of individual ones (#1255)
* run tests against all codegens instead of individual ones

* table testing

* fixture is no longer needed

Co-authored-by: Qing Ye <ye.qing@go-jek.com>
2020-10-09 14:04:12 +05:30

43 lines
1.2 KiB
JavaScript

export const CurlCodegen = {
id: "curl",
name: "cURL",
generator: ({
url,
pathName,
queryString,
auth,
httpUser,
httpPassword,
bearerToken,
method,
rawInput,
rawParams,
rawRequestBody,
contentType,
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)) {
const requestBody = rawInput ? rawParams : rawRequestBody
requestString.push(` -H 'Content-Type: ${contentType}; charset=utf-8'`)
requestString.push(` -d '${requestBody}'`)
}
return requestString.join(" \\\n")
},
}