CURL API to pass parameters in Bash script

2.5k views Asked by At

Curl command to pass arguments in the ICINGA API:

I have a curl command and passing it on a Bash script, I need to have two variables in the POST method for this URL, How do i pass the parameter to the CURL command

curl -k -s -u 'root:icinga' -H 'Accept: application/json' \
  -X POST 'https://sample.com:5665/v1/actions/acknowledge-problem?type=Service' \
  -d '{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {\"$1\} && service.name == {\"$2\}"" }''' \
  | python -m json.tool

The $1 and $2 should have hostname and servicenames respectively

Please help

thanks Aravind

1

There are 1 answers

2
larsks On

If you use single quotes ('like this') in bash, you get a literal string with no variable expansion. That is, compare:

$ echo '$DISPLAY'
$DISPLAY

With:

$ echo "$DISPLAY"
:0

This is exactly the same situation as in your curl command line, where you have:

'{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {\"$1\} && service.name == {\"$2\}"" }'''

There's actuall a number of quoting problems there starting with the ''' at the end, and including the "" just before the final }. If you want those variables to expand you will need to move them outside of your single quotes. You can do this:

'"host.name == {"'"$1"'"} && ...'

In this case, the "$1" is outside of the single quotation marks. Alternatively, you can do this:

"\"host.name == {\"$1\"} ** ..."

Here we're just using double quotes on the outside, so variable expansion works normally, but we have to escape every literal " inside the string.

Using the first option, the final argument to -d would look like something like this ("something" because I'm not familiar with icinga):

'{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {"'"$1"'"} && service.name == {"'"$2"'"}}'

If $1 is foo and $2 is bar, this gives you:

{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {"foo"} && service.name == {"bar"}}