Repository: louislam/uptime-kuma
File: src/pages/EditMonitor.vue (Vue)

Code before:
            if (this.monitor.headers) {
                try {
                    const jsonParsed = JSON.parse(this.monitor.headers);
                    for (const [ key, value ] of Object.entries(jsonParsed)) {
                        headers += ` -H '${key}: ${value}'`;
                    }
                } catch (e) {
                    headers = ` -H '${this.monitor.headers.replace(/\n/g, "")}'`;
                }
            }
            if (this.monitor.body && this.monitor.httpBodyEncoding === "json") {
                let json = "";
                try {
                    const jsonParsed = JSON.parse(this.monitor.body);
                    json = JSON.stringify(jsonParsed);
                    body = ` --json '${json}'`;
                } catch (e) {
                    json = this.monitor.body.replace(/\n/g, "");
                    body = ` --json '${json}'`;
                }
                method = "";
            } else if (this.monitor.body && this.monitor.method === "POST" && this.monitor.httpBodyEncoding === "xml") {
                headers += " -H 'Content-Type: application/xml'";
                body = ` -d ${this.monitor.body.replace(/\n/g, "")}`;
            } else if (this.monitor.body) {
                body = ` -d ${this.monitor.body.replace(/\n/g, "")}`;
            }
            if (this.monitor.authMethod === "basic") {
                basicAuth = ` -u ${this.monitor.basic_auth_user}:${this.monitor.basic_auth_pass}`;
            }
            return `curl -v${method}${this.monitor.timeout ? ` -m ${this.monitor.timeout}` : ""}${this.monitor.ignoreTls ? " -k" : ""}${basicAuth} '${this.monitor.url}'${headers}${body}`;
        },

        ipRegex() {

            // Allow to test with simple dns server with port (127.0.0.1:5300)
            if (! isDev) {
                return this.ipRegexPattern;
            }
            return null;
        },

        pageName() {
            let name = "Add New Monitor";
            if (this.isClone) {
                name = "Clone Monitor";
            } else if (this.isEdit) {
                name = "Edit";
            }
            return this.$t(name);
        },

Review comment (bug):
for the others, the escape hatch with newlines is reasonable.
For this one though, this would be incorrect (think of someone uploading csv's).

What do you think about using `"""` instead?
https://stackoverflow.com/a/76545799/8927366

Code after:
    },

    data() {
        return {
            modal: null,
            minInterval: MIN_INTERVAL_SECOND,
            maxInterval: MAX_INTERVAL_SECOND,
            processing: false,
            monitor: {
                notificationIDList: {},
                // Do not add default value here, please check init() method
            },
            acceptedStatusCodeOptions: [],
            dnsresolvetypeOptions: [],
            kafkaSaslMechanismOptions: [],
            ipOrHostnameRegexPattern: hostNameRegexPattern(),
            mqttIpOrHostnameRegexPattern: hostNameRegexPattern(true),
            gameList: null,
            connectionStringTemplates: {
                "sqlserver": "Server=<hostname>,<port>;Database=<your database>;User Id=<your user id>;Password=<your password>;Encrypt=<true/false>;TrustServerCertificate=<Yes/No>;Connection Timeout=<int>",
                "postgres": "postgres://username:password@host:port/database",
                "mysql": "mysql://username:password@host:port/database",
                "redis": "redis://user:password@host:port",
                "mongodb": "mongodb://username:password@host:port/database",
            },
            draftGroupName: null,
            remoteBrowsersEnabled: false,
        };
    },

    computed: {

        curlCommand() {
            const command = [ "curl", "--verbose", "--head", "--request", this.monitor.method, "\\\n", "--user-agent", userAgent, "\\\n" ];
            if (this.monitor.ignoreTls) {
                command.push("--insecure", "\\\n");
            }
            if (this.monitor.headers) {
                try {
                    // trying to parse the supplied data as json to trim whitespace
                    for (const [ key, value ] of Object.entries(JSON.parse(this.monitor.headers))) {
                        command.push("--header", `'${key}: ${value}'`, "\\\n");
                    }
                } catch (e) {
                    command.push("--header", `'${this.monitor.headers}'`, "\\\n");
                }
            }
            if (this.monitor.authMethod === "basic") {
                command.push("--user", `${this.monitor.basic_auth_user}:${this.monitor.basic_auth_pass}`, "--basic", "\\\n");
            } else if (this.monitor.authmethod === "mtls") {
                command.push("--cacert", `'${this.monitor.tlsCa}'`, "\\\n", "--key", `'${this.monitor.tlsKey}'`, "\\\n", "--cert", `'${this.monitor.tlsCert}'`, "\\\n");