Issue
I save a number out of a response from an earlier HTTP Request into a variable. In the CURL Request I need to insert this number into the URL, but somehow It doesn´t get recognized as a variable. It just gets sorted out which is why the URL is wrong and I get a 400 Error.
script{
// first httpRequest where I get the number from
def response = httpRequest httpMode: 'POST', url:"https://myServer.com", contentType: 'APPLICATION_JSON_UTF8', requestBody: 'myBody'
def props = readJSON text: response.content
def num = props.id[0] //num stores the number now
sh(Script: curl "https://myServer.com/$num/files" -i -X POST -H Content-Type:multipart/form-data -F [email protected]
}
The console output shows me that the sh tries to reach https://myServer.com//files , so it leaves out the variable.
I also tried to put the whole URL into a variable and refer in curl but it doesn´t get recognized as well. Of course I tried several typing options like ${num}, '$num' etc...
Any ideas?
EDIT
I also get the message "message":"The given url contains malicious characters" , probably because of the '$'
Another weird thing: If I type ${props.id[0]} instead of ${num} (which doesn´t make any difference because they hold the same number) the message changes to "/var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: 1: /var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: Bad substitution"
Solution
I solved the problem. the curl request needs to be surrounded by double quotation marks.
sh("curl https....")
The double quotation marks enable the possibility to use the variables defined in groovy script. Without them it just gets interpreted as java String.
Answered By - rw44