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?
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With