【问题标题】:How to retrieve the HTTP code from groovy curl execute method如何从 groovy curl 执行方法中检索 HTTP 代码
【发布时间】:2020-10-15 22:59:02
【问题描述】:

我在 Jenkins 管道代码中使用 groovy 执行 API 来执行 curl 命令,我从其余 API 获得响应,但我无法检索 HTTP 代码。

如何从 groovy 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)
}

【问题讨论】:

  • 什么是“HTTP 代码”——您是指正文/结果还是 HTTP 状态代码(例如 200 或 404)?
  • 我的意思是 HTTP 状态码,例如200 或 404
  • @cfrick,谢谢,但没有,有什么可以从 groovy 的进程对象中检索的
  • @nilesh1212,为什么不呢?这正是你所需要的......

标签: rest http curl groovy jenkins-pipeline


【解决方案1】:
    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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-15
    • 2012-03-12
    • 2019-01-09
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2021-08-09
    • 2014-11-21
    相关资源
    最近更新 更多