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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









