Executing a `curl` command with `ExecOperations` i...
# community-support
z
Executing a
curl
command with
ExecOperations
in Gradle. What is the best way to get the response values from it? specifically the json response body and http status code
e
do you need the exact status code, or is just ok/not-ok enough?
z
either would be fine tbh
e
curl from the past few years has a --fail-with-body option
so you can get both body (from output) and ok/not-ok (from exit code)
z
oh that’s interesting
currently doing:
Copy code
val standardOut = ByteArrayOutputStream()
exec.exec {
    // by default this would run in the sample app's project dir - we want the root project dir
    workingDir(rootProjectDirectory)
    val spec = this
    spec.commandLine(
        "curl",
        "-X",
        "POST",
        "-H",
        "Authorization: APIKey $dataTheoremApiKey",
        "--data",
        "\"\"",
        "<https://someUrl.com/v1/upload_init>"
    )
    standardOutput = standardOut
}
val output = standardOut.toString()
logger.lifecycle(output)
val json = output.substring(
    startIndex = output.indexOf("{ ")
        .also { startIndex ->
            if (startIndex == -1) {
                error("startIndex was not found")
            }
        },
    endIndex = output.indexOf(" }")
        .also { endIndex ->
            if (endIndex == -1) {
                error("endIndex was not found")
            }
        }
        // inclusive
        .let { endIndex -> endIndex + 2 },
)
but seems a bit obnoxious lol
e
why not just use HttpURLConnection
or java.net.http if you're on newer JDK
z
I agree, but the proxy at our company always behaves weird when we use jvm gradle code to make the network calls. from the command line (curl) it always just works