[SOLVED] Curl VS executeCommandLine VS JSON from coinmarketcap

Sorry to open another topic regardings curl and executeCommandLine
I now tried so many variations … i dont get it working

In terminal executed this

curl -H "X-CMC_PRO_API_KEY: MYPERSONALKEY" -H "Accept: application/json" -d "amount=1&symbol=ETH&convert=EUR" -G https://pro-api.coinmarketcap.com/v1/tools/price-conversion



i tried stuff like

var String ether = executeCommandLine("curl -H \"X-CMC_PRO_API_KEY: KEYKEY\" -H \"Accept: application/json\" -d \"amount=1&symbol=ETH&convert=EUR\" -G https://pro-api.coinmarketcap.com/v1/tools/price-conversion")
var String ether = executeCommandLine("curl@@-H@@\"X-CMC_PRO_API_KEY: KEYKEY\"@@-H@@\"Accept: application/json\"@@-d@@\"amount=1&symbol=ETH&convert=EUR\"@@-G@@https://pro-api.coinmarketcap.com/v1/tools/price-conversion")

Can someone try to help me to get this working?
Im not a very advanced user.

Afterwards i think i can pick out the JSON $.data.quote.EUR.price number by myself

What version of openHAB are you running? It’s no use trying old OH2 examples in OH3, for instance.

Im using OH3

Thanks in advance

The executeCommandLine Action changed for OH 3. Actions | openHAB. Each argument to the command is a separate argument to executeCommandLine now. No more @@. And if you actually want the result, you have to supply the timeout Duration.

1 Like

I also tried the comma separated Version but did Not exactly know how to handle the " and Spaces in Strings

But i will try again tomorrow and give Feedback

Please try

var String ether = executeCommandLine(Duration.ofSeconds(5), "curl", "-H", "X-CMC_PRO_API_KEY: KEYKEY", "-H", "Accept: application/json", "-d", "amount=1&symbol=ETH&convert=EUR", "-G", "https://pro-api.coinmarketcap.com/v1/tools/price-conversion")

1 Like

I have to say thank you!

The timeout-duration was the thing i was missing all the time!
In another post of yours i already found the info regardings the comma-separated synthax

Thanks for taking the time and providing the final solution

Works perfect now for me :star_struck:

Sorry one last question:
Its normal that the executeCommandLine now returns some sort of header:

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 323 0 323 0 0 1218 0 --:--:-- --:--:-- --:--:-- 1218 {"status":{"timestamp":"2021-03-02T16:51:45.669Z","error_code":0,"error_message":null,"elapsed":33,"credit_count":1,"notice":null},"data":{"id":1027,"symbol":"ETH","name":"Ethereum","amount":1,"last_updated":"2021-03-02T16:50:03.000Z","quote":{"EUR":{"price":1261.1368697478365,"last_updated":"2021-03-02T16:51:02.000Z"}}}}

So to be able to use JSON-transformation for getting my $.data.quote.EUR.price out of it i think i will use the following procedure

  1. curl information
  2. regex: ignore everything before the first “{”
  3. JSON-transformation $.data.quote.EUR.price

Due to my limited skills im not sure if there is an easier way
(Maybe for others reading this topic its also helpful)

Thanks a lot

That comes from curl. There is an option to suppress it but I don’t know it off the top of my head.

1 Like
  1. You are the man! → -s for “silent” does the job
  2. I could have found this by myself :zipper_mouth_face:

So for beginners like me, here is the working code:

// Example response
// {"status":{"timestamp":"2021-03-01T18:31:47.530Z","error_code":0,"error_message":null,"elapsed":19,"credit_count":1,"notice":null},"data":{"id":1027,"symbol":"ETH","name":"Ethereum","amount":1,"last_updated":"2021-03-01T18:30:03.000Z","quote":{"EUR":{"price":1260.4932810006726,"last_updated":"2021-03-01T18:31:03.000Z"}}}}

// Get JSON
var String ether = executeCommandLine(Duration.ofSeconds(5), "curl", "-s", "-H", "X-CMC_PRO_API_KEY: YOURPERSONALKEY", "-H", "Accept: application/json", "-d", "amount=1&symbol=ETH&convert=EUR", "-G", "https://pro-api.coinmarketcap.com/v1/tools/price-conversion")

// Get Price
ether = transform("JSONPATH","$.data.quote.EUR.price", ether)
// Write to Item

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.