I'm building a plugin in intellij and want to make a curl request. The plugin is written in kotlin.
The following curl request works:
val comd = arrayOf(
"curl",
"-X",
"GET",
"-H",
"Authorization: AUTH_TOKEN",
"https://api.bitrise.io/v0.1/me"
)
But the following POST request with data doesn't:
val comd = arrayOf(
"curl",
"-X",
"POST",
"https://api.bitrise.io/v0.1/apps/appslug/builds",
"-H",
"Authorization: AUTH_TOKEN",
" -d $data"
)
I'm using this function to run the request:
private fun executeCommandLine(project: Project, comd: Array<String>) {
val process = Runtime.getRuntime().exec(comd)
val processHandler: ProcessHandler = OSProcessHandler(process, "clear", Charset.forName("UTF-8"))
consoleView.attachToProcess(processHandler)
processHandler.startNotify()
}
It just throws an error:
curl: (3) nested brace in URL position 18:
-d {"hook_info":{"type":"bitrise"},"build_params":{"branch":"master"}}
I've tried running the request with -g switch for globbing error. But then I run into another one:
curl: (3) URL using bad/illegal format or missing URL
What am I missing here?
PS: I've had to break my curl command into an array otherwise it doesn't work. Check this: Unable to execute CURL command through java Runtime.getRuntime().exec()
Thanks in advance :)
Return the curl command to a string and then try using this function from @leondepeon's answer
/** Run a system-level command.
* Note: This is a system independent java exec (e.g. | doesn't work). For shell: prefix with "bash -c"
* Inputting the string in stdIn (if any), and returning stdout and stderr as a string. */
fun exec(cmd: String, stdIn: String = "", captureOutput:Boolean = false, workingDir: File = File(".")): String? {
try {
val process = ProcessBuilder(*cmd.split("\\s".toRegex()).toTypedArray())
.directory(workingDir)
.redirectOutput(if (captureOutput) ProcessBuilder.Redirect.PIPE else ProcessBuilder.Redirect.INHERIT)
.redirectError(if (captureOutput) ProcessBuilder.Redirect.PIPE else ProcessBuilder.Redirect.INHERIT)
.start().apply {
if (stdIn != "") {
outputStream.bufferedWriter().apply {
write(stdIn)
flush()
close()
}
}
waitFor(60, TimeUnit.SECONDS)
}
if (captureOutput) {
return process.inputStream.bufferedReader().readText()
}
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
Also, if working on Windows, be wary of the single quote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With