1

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)
}
nilesh1212
  • 1,561
  • 2
  • 26
  • 60

1 Answers1

2
    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()
MaratC
  • 6,418
  • 2
  • 20
  • 27