Kerkael Belarpaillon
Kerkael Belarpaillon

Reputation: 109

curl metrics variables and dimensions

Hi and thanks for your help. I try to pass pair of arguments in a curl command. I already found out how to pass single variables, like my MetricType, MetricName, current time and the value from $1.

I can pass both the Dim1Name AND the value, like this:

curl --header "Content-Type: application/json"
--header "X-SF-TOKEN: ${TOKEN}" -X POST -i https://URL/datapoint
--data '{"'"${MetricType}"'": [{ "metric": "'"${MetricName}"'","value": 300,

    "dimensions": {"'"${Dim1Name}"'": "'"${Dim1Value}"'"}

    }]}'

What I'd like to achieve is something like this, where DimOption=Dim1Name: Dim1Value so the code would look like this (but the Name and Value need quotes). Is it possible ? (And with a coma as well in case I want to send 2+ pairs of arguments)

curl --header "Content-Type: application/json"
--header "X-SF-TOKEN: ${TOKEN}" -X POST -i https://URL/datapoint
--data '{"'"${MetricType}"'": [{ "metric": "'"${MetricName}"'","value": 300,

    "dimensions": {"'"${DimOption}'""}

    }]}'

Upvotes: 0

Views: 33

Answers (1)

JRichardsz
JRichardsz

Reputation: 16495

Multi line bash script

You can use a multi-line script for easier handling of the body and without worry about the quotes scape:

send.sh

data=$(cat data.json)

new_data=${data/@name/jane}
new_data=${new_data/@last_name/doe}

curl localhost:8080/foo -d "$new_data" -H "Content-Type: application/json"
echo

data.json

{
    "hello": "@name @last_name"
}

result

enter image description here

Explanation

  • We are reading a data.json file with the json content
  • Inside of data.json you can use placeholders to make it dynamic
    • @name
    • @last_name
  • Then using bash, we are replacing the place holders with the desired values
  • Finally the evaluated json string is sent to the curl

Upvotes: 2

Related Questions