0

I am trying to execute the curl command from the groovy script on Jenkins. When I ran the script from the machine it is returning expected result. When I am trying to execute the same from Jenkins groovy it is giving null output.

The curl part that works when executed from the machine's command line:

authID=$(curl -sk $zabbixurl -H "Content-Type: application/json" -d "{\"jsonrpc\": \"2.0\", \"method\": \"user.login\", \"params\": { \"user\": \"$zabbixusername\", \"password\": \"$zabbix_password\" }, \"id\": 1}" )

The script I execute from Groovy:

def zabbix(){
sh """
AuthID=\$(curl -sk "\$zabbixurl" -H \\\"Content-Type: application/json\\\" -d \\\'{\\\"jsonrpc\\\":\\\"2.0\\\",\\\"method\\\":\\\"user.login\\\",\\\"id\\\":1,\\\"params\\\":{\\\"user\\\":\\\"\$zabbixusername\\\",\\\"password\\\":\\\"\$zabbix_password\\\"}}\\\' )
echo \$AuthID
"""
}
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Do not use double quotes inside it. Switch `"` for `'` and in the variable do `${zabbixurl}` instead of `"\$zabbixurl"` – samthegolden Jan 10 '20 at 08:33
  • Possible duplicate of https://stackoverflow.com/questions/51492967/how-catch-curl-response-into-variable-in-jenkinsfile/51493308#51493308 – Szymon Stepniak Jan 10 '20 at 08:35
  • zabbix_url i am assigning value from shell . i changed the script like below and no result AuthID=\$(curl -sk \\\'\$zabbixurl\\\' -H \\\'Content-Type: application/json\\\' -d \\\'{\\\"jsonrpc\\\":\\\"2.0\\\",\\\"method\\\":\\\"user.login\\\",\\\"id\\\":1,\\\"params\\\":{\\\"user\\\":\\\"\$zabbixusername\\\",\\\"password\\\":\\\"\$zabbix_password\\\"}}\\\' | jq -r .result) echo \$AuthID – syendra Jan 10 '20 at 08:49

0 Answers0