I'm getting this Warning: A secret was passed to "httpRequest" using Groovy String interpolation, which is insecure using the first example here. I made this keyvar = credentials('key_id') as an environmental variable and put it in something like this
def response = httpRequest url: "https://url...",
customHeaders: [[name: 'Authorization', value: "${keyvar}"]]...
Which works but is not how it should be properly done as described in this documentation, so following that I tried what it suggested here, using single quotes and no bracket.
def response = httpRequest url: "https://url...",
customHeaders: [[name: 'Authorization', value: '$keyvar']]...
This solves the first error but now I get Response Code: HTTP/1.1 401 Unauthorized which to me, means that interpolation isn't working within the single quotes as the documentation describes.
To make the answer from Matt Schuchard in the comments more clear for future people stumbling upon this - you need to remove the string interpolation and pass the variable directly:
def response = httpRequest url: "https://url...",
customHeaders: [[name: 'Authorization', value: keyvar]]...
This also works for headers that require an additional prefix (e.g. Bearer):
def response = httpRequest url: "https://url...",
customHeaders: [[name: 'Authorization', value: 'Bearer ' + keyvar]]...
is safe, while
def response = httpRequest url: "https://url...",
customHeaders: [[name: 'Authorization', value: "Bearer $keyvar"]]...
is not.
Instead of httpRequest method, I have the aliOssUpload method. It doesn’t work by passing the argument directly to the argument without recasting as a string.
The interpolation warning can be resolved by escaping the $\keyvar
Related
In a delcarative pipeline, I am making an POST API request that requires a file within the request body for uploading. The request looks something like this:
def response = httpRequest url: "https://cloud.tenable.com/compliance/export",
customHeaders: [[name: 'X-ApiKeys', value: "${ACCESS_KEY}"]],
httpMode: 'POST',
acceptType: 'APPLICATION_JSON',
contentType: 'multipart/form-data',
requestBody:"{"Filedata":????}"
This is made with the HTTP Request Plugin and this API Syntax. Assuming I have a zip file called test.zip in my workspace directory, how can I "read" the zip file into the request for uploading? Thanks for the help.
I am trying to write a groovy script for my Jenkins pipeline which calls an API that outputs a '.xls' file and store it in the workspace directory.
I used the pipeline syntax generator to generate a script for HttpRequest which is as shown below.
CODE:
def response = httpRequest customHeaders: [[maskValue: false, name: 'Accept', value: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']], outputFile: './abc.xls', url: 'http://xyz/', wrapAsMultipart: false
The above-mentioned code is able to download the file at the required location, but the file data is corrupted.
I tried using the default content-type/Accept available in Jenkins and even tried custom headers, but none of them seem to be able to retrieve the correct '.xls' file data.
When trying to hit the API with PostMan using Accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' in the header, the file data is received in its correct format, the file is not corrupted.
Can anyone help me in figuring out what might be the exact issue here?
contentType sets the request format, while acceptType defines the response format. Try setting the acceptType as follows:
def response = httpRequest url: "https:/....", httpMode: 'POST',
contentType: 'APPLICATION_JSON',
requestBody: JsonOutput.toJson(bodyParameters),
acceptType: 'APPLICATION_OCTETSTREAM',
outputFile: "${REPORT_FILE_NAME_COMPLETE_PATH}"
I am trying to call a POST api in the Jenkins groovy post-build section. I want to pass a groovy variable in the json body of the request.
def url = "someURL"
def body = '[{"PageName" :"worked2","pageurl" :"url variable value needs to be passed here"}]'
def http = new URL("some https url").openConnection();
http.setRequestMethod("POST")
http.setDoOutput(true)
http.setRequestProperty("Accept", "application/json")
http.setRequestProperty("Content-Type", "application/json")
http.getOutputStream().write(body.getBytes("UTF-8"));
http.connect()
def postRC = http.getResponseCode();
I have tried below but nothing worked for me:
'[{"PageName" :"worked2","pageurl" :$url}]'
'[{"PageName" :"worked2","pageurl" :"$url"}]'
'[{"PageName" :"worked2","pageurl" :url}]'
It only works fine when I hard-code the value. How can I achieve this?
This worked when I used below :
{"pageurl" :"'+ url +'"}
(double quotes single quotes + variableName + single quotes double quotes)
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.
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}"
}
}
}
}
You want to:
Read the file with readFile
Use it as the responseBody in the httpRequest call
Don't forget to set the content type and http headers appropriately.
I am trying to pass a URL as parameter to the httpRequest plugin in a jenkins pipeline , but it couldnt replace the paramater with its value
httpRequest authentication: 'credauth', httpMode: 'POST', responseHandle: 'NONE', url: '${params.URL}'
instead of ${params.URL} i also tried env.URL, ${URL}, param.URL nothing seem to work
Any help is appreciated
For the groovy variable substitution, enclose them under double-quotes
httpRequest authentication: 'credauth', httpMode: 'POST', responseHandle: 'NONE', url: "${params.URL}"