Issue
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?
Solution
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}"
Answered By - Zecas