Security

"Token is required" error when executing curl command from Java's ProcessBuilder

angrydead
Explorer

I am using Java's ProcessBuilder to execute curl from Java. The same command being executed in bash works just fine, but I get a {"text": "Token is required", "code":2} error from Java.

public String call() throws IOException, InterruptedException {
    List<String> command = new ArrayList<>();

    command.add("curl");
    command.add("-X");
    command.add(method.name());
    command.add("-k");
    command.add(endpoint);

    if (headers != null && !headers.isEmpty()) {
        StringBuilder builder = new StringBuilder();
        headers.keySet().forEach(s -> {
            builder.replace(0, builder.length(), "");
            builder.append("'").append(s).append(":").append(headers.get(s)).append("'");
            command.add("-H");
            command.add(builder.toString());
        });
    }

    if (data != null) {
        command.add("-d");
        command.add("'" + data + "'");
    }

    System.out.println(StringUtils.join(command, " "));

    return doCurl(command.toArray(new String[0]));
}

private String doCurl(String[] args) throws IOException, InterruptedException {
    Process process = new ProcessBuilder(args)
            .redirectErrorStream(true)
            .start();

    String lines;
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"))) {
        lines = reader.lines().collect(Collectors.joining("\n"));
    }
    process.waitFor();
    return lines;
}
Tags (1)
0 Karma
Get Updates on the Splunk Community!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...