Dlacreme

Dlacreme

Can't properly format CURL parameters with `System.cmd/2`

Hi guys !

I need to run a one-time HTTP query in one of my projects, it needs to include a PEM certificate and since I was not able to find any documentation on how to do it with either hackney or mint, I decided to do it using System.cmd/2.

The valid CURL command looks like this:

curl --request POST \
--url https://xxxx \
--cert ./certdir/cert.pem \
--key ./certdir/cert-key.pem \
--verbose -H "Content-Type: application/json" \
-d '{"merchantIdentifier": "xxx", "domainName":"xxx","displayName":"xxx"}'

This works as expected. However, I tried many things but was not able to run the same command from Elixir. I tried as a single string, I tried with ~s, ~w etc… and I always end up with unmatched close brace/bracket in URL position 2: error.

This is what it looks like:

    {res, _n} = System.cmd("curl", ~w[
        https://xxx
				--request POST
				--cert ./certdir/cert.pem
				--key ./certdir/cert-key.pem
				-H 'Content-Type: application/json'
				-d '{"merchantIdentifier": "xxx", "domainName":"xxx","displayName":"xxx"}
      ])

Any idea why it fails? I never got any issue with this and I am honestly confused about what’s happening and very frustrated to get stuck on such a small issue.

Thank you!

Most Liked

cmo

cmo

You might have some luck with ssl and httpc from Erlang.

And you can’t use w if your args have spaces in them. Try a handcrafted list of strings.

Plus, you’re missing a single quote at the end.

stefanchrobot

stefanchrobot

This is not producing the result you’re expecting. 'Content-Type: application/json' is getting split wrongly. I suggest using explicit strings:

[
  "https://xxx",
  "--request",
  "POST",
  "--cert",
  "./certdir/cert.pem",
  "--key",
  "./certdir/cert-key.pem",
  "-H",
  "'Content-Type: application/json'",
  "-d",
  "'{\"merchantIdentifier\": \"xxx\", \"domainName\": \"xxx\", \"displayName\": \"xxx\"}"
]

You can use the ~s() sigil for the last param to make it more readable.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement