Search code examples
curlcommandsystemelixir

Can't run curl command from within Elixir with System.cmd


I'm just trying to use Elixir to run a curl command with a specific format.

$ curl -w "@config/curl-format.txt" -o /dev/null -s "http://wordpress.com/"
0.004, 0.017, 0.000, 0.017, 0.000, 0.029, 0.029

Running the command directly from the terminal works fine.


This is what I'm trying to do in Elixir:

args = ["-w config/curl-format.txt", "-o /dev/null", "-s", "http://wordpress.com"]
result = System.cmd("curl", args, [])

But I get:

{" config/curl-format.txt", 23}

And not the same result as above.


Solution

  • Your System.cmd invocation is equivalent (in shell syntax) to:

    curl "-w config/curl-format.txt" "-o /dev/null" -s http://wordpress.com
    

    You need to pass in -w, config/curl-format.txt, -o, and /dev/null as different arguments. You also missed the @ in @config/curl-format.txt. This should work:

    args = ["-w", "@config/curl-format.txt", "-o", "/dev/null", "-s", "http://wordpress.com"]
    result = System.cmd("curl", args, [])