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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement