我在 Jenkins 管道代码中使用 groovy execute API 来执行 curl 命令,我从其余 API 得到响应,但我无法检索 HTTP 代码。
如何从时髦的curl执行方法中检索HTTP代码。
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)
}
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()