The provided code doesn’t work due to a bug in Jenkins’ core (this issue was marked as fixed in jenkins >= 2.332.1LTS or 2.335)
As a workaround I can use:
def sendApiRequest(String query, String httpMethod, String contentType ="", String data =""){
/*
Caller must wrap function call with 'withCredentials' statement
with 'USERNAME' and 'PASSWORD' environment varaiables
*/
String apiCmd = """curl -s -w '###%{http_code}' -X '${httpMethod}' \
'${this.protocol}://$query' \
${contentType ? "-H 'Content-Type: $contentType'" : ''} \
-u "\$USERNAME:\$PASSWORD" \
${data ? "-d '${data}'" : ''}"""
def response = steps.sh(returnStdout: true, script: apiCmd, label: "Send API request to Artifactory").trim()
def responseCode = response.split("###")[1]
def responseData = response.split("###")[0]
try{
responseCode = responseCode.toInteger()
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid status code: `$responseCode` cannot cast to integer")
}
return [responseCode, responseData]
}