如何解决从Jenkins管道上的http请求中提取JSON响应
我正在使用jenkins为我的应用创建配置项
以下是我在构建应用后调用的另一个脚本
script{
sh 'curl -X POST -H "Authorization:test "https://api/upload" -F "file=@path"'
}
上面的脚本将返回json响应,如何从json中提取ID字段并将其存储在变量中?
解决方法
尝试一下,尚未测试。
script{
sh 'ID=$(curl -X POST -H "Authorization:test "https://api/upload" -F "file=@path")'
sh 'curl -X POST -H "Authorization:test" -F app_id=$ID -H "content-type: multipart/form-data" https://api/tasks'
}
,
您可以借助returnStdout:true
将输出分配给变量。
pipeline {
agent any
stages {
stage('test') {
steps {
script{
def output = sh returnStdout:true,script: '''
curl -X POST -H "Authorization:test" \
"https://api/upload" -F "file=@path"
'''
sh """
curl -X POST -H "Authorization:test" -F app_id=$ID \
-H "content-type: multipart/form-data" \
https://api/tasks
"""
}
}
}
}
}
然后将该变量替换在双引号内。
,这是我从json响应中获取ID的方法。
def response = sh(script: 'curl -X POST -H "Authorization:test" -H "content-type: multipart/form-data" https://api/upload',returnStdout: true)
def responseObject = readJSON text: response
def ID = "$responseObject.id"
println("ID: $ID")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。