Home > Software design >  Simple and double quotes in cURL in groovy
Simple and double quotes in cURL in groovy

Time:11-30

I have a Jenkins pipeline where this command works and send me a notification through google chat :

script {
    sh 'curl -k "https://chat.googleapis.com/v1/spaces/AAAABHT3HT0/messages?key=*****&token=******" -d "@chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}

But if I enter the url in a variable, that does not work any more :

script {
    url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
    sh 'curl -k ${url} -d "@chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}

With the error :

curl -k -d @chat_notification.json -XPOST -H 'Content-Type: application/json; charset=UTF-8'
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

It's probably a quote issue ?

CodePudding user response:

Yes, if you are using single quotes in Groovy, that means you cannot interpolate the string with variables using the $ syntax.
See https://groovy-lang.org/syntax.html#_string_interpolation .
Same thing applies to shell code, by the way, or sh in this case. But since you are not using any shell variables in your code, simply swapping the quotes, i.e. quoting your Groovy string with ", and using ' within the curl command would probably work.

CodePudding user response:

Yes it is a quote issue try to use "" instead of '' to use the value of url variable in curl command. You should also seperate -X and POST (you have -XPOST in your script)

script {
    url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
    sh "curl -k ${url} -d @chat_notification.json -X POST -H Content-Type: application/json; charset=UTF-8"
}
  • Related