nilesh1212
nilesh1212

Reputation: 1655

How to retrieve the HTTP code from groovy curl execute method

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

Answers (1)

MaratC
MaratC

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

Related Questions