I need to make API call with payload (everything needs to be exactly like that)
{"file": "//'HLQ.DATASET(MEMBER)'"}
in Jenkins pipeline. I can't figure correct escaping of the payload. Problem is with the round brackets, single escape - Groovy complains, double escape - one of the slashes bubbles all the way into curl call.
def String job = """{\\"file\\": \\"\\'HLQ.DATASET\\(MEMBER\\)\\'\\"}"""
...
script {
def String response = sh(script: " curl -X PUT -w %{http_code} -v --header 'Content-Type: application/json' --cookie cookies.txt --header 'X-CSRF-ZOSMF-HEADER: dummy' --header 'X-IBM-Notification-URL: ${hook.getURL()}' https://.../zosmf/restjobs/jobs --data '$job'", returnStdout: true).trim()
}
CodePudding user response:
If you are checking the Jenkins console output to determine whether the message is correctly sent it would mislead you. What you see in the console output is not always the interpreted string.
Can you try something like the below? Also inorder to check what Curl is sending out you can use a flag like --trace
def String job = "{\"file\": \"//'HLQ.DATASET(MEMBER)'\"}"
writeFile(file: 'payload.txt', text: job)
sh 'cat payload.txt'
def String response = sh(script: "curl -X PUT -w %{http_code} -v --header 'Content-Type: application/json' --cookie cookies.txt --header 'X-CSRF-ZOSMF-HEADER: dummy' --header 'X-IBM-Notification-URL: ${hook.getURL()}' https://.../zosmf/restjobs/jobs --data @payload.txt", returnStdout: true).trim()