Jenkins http request not recognizing filename

Viewed 438

I think this request fails because it cannot recognize the filename. Using this HTTP Request Plugin and following this API documentation in a declarative pipeline, I ran the request below...

script {
       env.WORKSPACE = pwd()
       
       def response = httpRequest url: "https://cloud.tenable.com/compliance/export", 
           customHeaders: [[name: 'X-ApiKeys', value: "${ACCESS_KEY}"]],
           httpMode: 'POST',
           acceptType: 'APPLICATION_JSON',
           uploadFile: "${env.WORKSPACE}/test.zip"
         }        

Error:

java.lang.IllegalStateException: Name is blank
11:02:40    at org.apache.http.util.Asserts.notBlank(Asserts.java:64)
11:02:40    at org.apache.http.entity.mime.FormBodyPartBuilder.build(FormBodyPartBuilder.java:96)
11:02:40    at org.apache.http.entity.mime.MultipartEntityBuilder.addPart(MultipartEntityBuilder.java:148)
11:02:40    at jenkins.plugins.http_request.HttpRequestExecution.authAndRequest(HttpRequestExecution.java:314)
11:02:40    at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:260)
11:02:40    at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:80)
11:02:40    at hudson.remoting.LocalChannel.call(LocalChannel.java:46)
11:02:40    at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:380)
11:02:40    at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:358)
11:02:40    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
11:02:40    at hudson.security.ACL.impersonate2(ACL.java:449)
11:02:40    at hudson.security.ACL.impersonate(ACL.java:461)
11:02:40    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
11:02:40    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
11:02:40    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11:02:40    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
11:02:40    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
11:02:40    at java.lang.Thread.run(Thread.java:748)

I believe this is related to the addFile method in the java docs? Specifying the contentType to APPLICATION_ZIP or APPLICATION_OCTETSTREAM results in the same error. Replacing test.zip with a non-existent file changes the error to 'file does not exist', which suggests to me that test.zip is being read but the 'name' param is somehow blank.

1 Answers

Apparently you have to set multipartName:

httpRequest url: "https://cloud.tenable.com/compliance/export", 
    customHeaders: [[name: 'X-ApiKeys', value: "${ACCESS_KEY}"]],
    httpMode: 'POST',
    acceptType: 'APPLICATION_JSON',
    uploadFile: "test.zip", // env.WORKSPACE is not necessary, the path is already relative to the current workspace 
    multipartName: "something" // <--

(I belive this sets the name of the HTML form field the file is associated with. For a REST API this value doesn't matter I guess.)


Alternatively, if your server supports it, you can also set wrapAsMultipart to false so the file will not be wrapped as multipart/form-data but sent directly in the body of the request.

Related