Samuel-88

Samuel-88

I can't translate a terminal command to System.cmd/3

Hello all,

I’m trying to run the following terminal command inside Elixir:

curl -X POST http://localhost:4004/api/converter -H "Content-Type: application/json" -d '{"html": "a", "organization_id": "9213898231sadasd000"}'

It executes perfectly in the terminal, but when I try it in iex like this:

System.cmd("curl", ~w[-X POST http://localhost:4004/api/converter -H "Content-Type: application/json" -d '{"html": "a", "organization_id": "9213898231sadasd000"}'])

I get a bad request. I already read the documentantion and looked how different people did it, but I can’t make it work. Does anyone have any idea what I’m doing wrong?

Thanks! :smiley:

Most Liked

al2o3cr

al2o3cr

When you input a command in the shell, the shell is responsible for taking the whole string you typed:

curl -X POST http://localhost:4004/api/converter -H "Content-Type: application/json" -d '{"html": "a", "organization_id": "9213898231sadasd000"}'

and converting it to a command (curl) along with a series of options, separated by spaces. Most of the quote-marks used above are to help the shell split the values correctly. (see below)

System.cmd/2 operates at a level of abstraction lower than that - it expects the caller to provide the command as a string, and the arguments as a list of strings. Because of that, most of the quotes are not only not required, but could cause bugs. For instance:

-H "Content-Type: application/json"
# parsed by the shell as two arguments:
# -H
# Content-Type: application/json

But passing ["-H", "\"Content-Type: application/json\""] to System.cmd will send the double-quotes along to the program, which isn’t the intent.


For the input given, all of these quote-marks are formatting for the shell that isn’t wanted in the actual data sent to curl:

curl -X POST http://localhost:4004/api/converter -H "Content-Type: application/json" -d '{"html": "a", "organization_id": "9213898231sadasd000"}'
                                                    ^                              ^    ^                                                       ^
al2o3cr

al2o3cr

As noted in this thread from last week:

~w() will not do what you want if arguments have spaces in them:

~w(-H "Content-Type: application/json")

is a list with three elements:

["-H", "\"Content-Type:", "application/json\""]
hauleth

hauleth

You do not need the quotes inside quotes. These are just for shell, not for cURL

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement