Reputation: 939
I'm creating a CI for my app using jenkins
Below is an additional script I call after building my app
script{
sh 'curl -X POST -H "Authorization:test "https://api/upload" -F "file=@path"'
}
Above script will return json response, how can I extract the ID field from json and store it on a variable?
Upvotes: 1
Views: 3688
Reputation: 939
Here's how I got the ID from json response.
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")
Upvotes: 2
Reputation: 670
You can assign the output to a variable with the help of 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
"""
}
}
}
}
}
The variable is then replaced inside the double quotes.
Upvotes: -1
Reputation: 11772
Try it, not yet tested.
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'
}
Upvotes: -1