Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response code from curl in a jenkinsfile

In a jenkinsfile I call curl with:

sh "curl -X POST -i -u admin:admin https://[myhost]"

and I get output like this:

...
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
...

I would like to take different actions based on the response code from the above call but how do I store only the response code/reply in a variable?

like image 597
u123 Avatar asked Mar 07 '23 22:03

u123


1 Answers

By using the parameter -w %{http_code}(from Use HTTP status codes from curl)

you can easily get the HTTP response code:

int status = sh(script: "curl -sLI -w '%{http_code}' $url -o /dev/null", returnStdout: true)

if (status != 200 && status != 201) {
    error("Returned status code = $status when calling $url")
}

like image 167
LE GALL Benoît Avatar answered Mar 16 '23 00:03

LE GALL Benoît