Reputation: 1655
I am using groovy execute API in Jenkins pipeline code to execute the curl command, I am getting response from the rest API, but I am not able to retrieve HTTP code.
How to retrieve the HTTP code from groovy curl execute method.
node{
stage("api")
{
getApi()
}
}
def getApi()
{
def process = ['bash', '-c',"curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/json' -H 'Authorization: Bearer oosfjwejowjefojwoejfowoefjwojefwefweofjwo' https://myrest.api.com"].execute()
process.waitFor()
println (process.err.text)
println (process.text)
}
Upvotes: 1
Views: 2873
Reputation: 6859
def http_code = sh(
returnStdout: true,
label: "checking myrest.api.com",
script: """curl -X GET --header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-H 'Authorization: Bearer oosfjwejowjefojwoejfowoefjwojefwefweofjwo' \
https://myrest.api.com -o /dev/null -w '%{http_code}'"""
).trim()
Upvotes: 2