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

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
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
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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 30048 115
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement