在使用 curl 的 Jenkins 管道中如何存储响应 cookie?

In Jenkins pipeline using curl how can store response cookies?

我正在使用带有 curl 的 sh 命令编写 Jenkins 管道。

使用常规命令提示符可以存储响应 cookie,然后在另一个请求中发送这些 cookie,例如存储 cookie

curl -k -c my_cookies.txt -X POST https://myLogin

然后使用这些 cookie 发送请求

curl -k -b my_cookies.txt -X GET https://myGetRequest

Jenkins Pipeline 是否可能有类似的东西?

我试过类似的东西:

final String url = "https://myLogin"
final String response = sh(script: "curl -c my_cookie.txt -k -X POST $url ", returnStdout: true).trim()

但这会产生类似

的错误
curl: (35) Encountered end of file

感谢任何帮助。

可以这样做:

pipeline {
    agent any;
    stages {
        stage('call 1st api') {
            steps {
                sh """
                    curl --cookie-jar $WORKSPACE/cookie.txt https://62e96ddc0c77.ngrok.io/put
                """
            }
        }
        stage('call 2nd api') {
            steps {
                sh """
                    curl --cookie $WORKSPACE/cookie.txt https://62e96ddc0c77.ngrok.io/get 
                """
            }
        }
    }
}

在上面的示例中,如果第一次调用成功,cookie 将存储在 $WORKSPACE/cookie.txt 位置,第二次调用将从同一位置读取它。 $WORKSPACE - 是作业工作区的位置。您无需对位置进行硬编码,因为 Jenkins 会处理它