Reputation: 91
I would like to make a call to an RESTful endpoint from a Jenkins pipeline using the contents of a file from my workspace as the body. I am trying to use the HTTP Request Plugin (https://plugins.jenkins.io/http_request) but can't figure out how to do this. Basic flow: 1) Get file from source control, i.e. GitHub 2) Use shell script to update the file in my workspace with sed 3) Use the file as the body of an HTTP request call
I have been trying to copy the contents of the file to a variable and then using that in the request but thats not working as I can't seem to figure out how to save the file contents to a variable and then reference it in the next step.
Upvotes: 2
Views: 5706
Reputation: 91
Here is a simple pipeline that I now have working that can be used as an example if anyone else is trying to achieve this. Note I had to adjust the code that Blue Ocean created as it put the environment variable in single quotes.
pipeline {
agent any
stages {
stage('stage1') {
steps {
httpRequest(url: 'http://banka.mybluemix.net/loans/v1/quote?loanAmount=9501.64&annualInterestRate=28&termInMonths=36', acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'STRING', validResponseCodes: '200', outputFile: 'body.json')
script {
env.requestBody = readFile 'body.json'
}
echo "${env.requestBody}"
httpRequest(url: 'https://postman-echo.com/post', acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', outputFile: 'postmanOutput.txt', requestBody: "${env.requestBody}", responseHandle: 'STRING', validResponseCodes: '200')
script {
env.POSTMANOUT = readFile 'postmanOutput.txt'
}
echo "${env.POSTMANOUT}"
}
}
}
}
Upvotes: 3
Reputation: 1923
You want to:
Don't forget to set the content type and http headers appropriately.
Upvotes: 1