piecia
piecia

Reputation: 386

Jenkins - execute curl command with options from multi-line parameter

I have a job with multi-line parameter:

parameters {
  text name: 'CURL_OPTIONS', defaultValue: '', description: 'The curl options and parameters.'
}

I'm trying to execute curl command with given parameter, for example:

--location 'https://ifconfig.io' \
 --header 'header1: X' \
 --header 'header2: Y' \
 --header 'header3: Z' \
 --header 'Content-Type: application/json' \
 --data '{
     "data": {
         "app": "X",
         "countryRegion": "EN"
     }
 }'

which comes from Postman code snippet.

In the jenkinsfile I have:

sh '''
  curl $CURL_OPTIONS
'''

But it does not work:

curl: (1) Protocol 'https not supported or disabled in libcurl
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: \; Unknown error
 curl: (6) Could not resolve host: X'; Unknown error
 curl: (6) Could not resolve host: \; Unknown error
 curl: (6) Could not resolve host: Y'; Unknown error
 curl: (6) Could not resolve host: \; Unknown error
 curl: (6) Could not resolve host: Z'; Unknown error
 curl: (6) Could not resolve host: \; Unknown error
 curl: (6) Could not resolve host: application; Unknown error
 curl: (6) Could not resolve host: \; Unknown error
 curl: (6) Could not resolve host: "data"; Unknown error
 curl: (3) [globbing] unmatched brace at pos 2
 curl: (6) Could not resolve host: "app"; Unknown error
 curl: (6) Could not resolve host: "X",; Unknown error
 curl: (6) Could not resolve host: "countryRegion"; Unknown error
 curl: (6) Could not resolve host: "EN"; Unknown error
 curl: (3) [globbing] unmatched close brace/bracket at pos 1
 curl: (3) [globbing] unmatched close brace/bracket at pos 1

It also does not work even if pass tr command to remove backslash and '\n':

curl $( echo $CURL_OPTIONS | tr -d '\n\\' )

It is familiar to this:

$ cat curl_options1.txt | tr -d '\n\\'
--location 'https://ifconfig.io'  --header 'header1: X'  --header 'header2: Y'  --header 'header3: Z'  --header 'Content-Type: application/json'  --data-raw '{     "data": {         "app": "X",         "countryRegion": "EN"     } }'

$ curl $(cat curl_options1.txt | tr -d '\n\\' )
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: X'
curl: (6) Could not resolve host: Y'
curl: (6) Could not resolve host: Z'
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched brace in URL position 1:

Of course it works from command line:

$ curl --location 'https://ifconfig.io' \
  --header 'header1: X' \
  --header 'header2: Y' \
  --header 'header3: Z' \
  --header 'Content-Type: application/json' \
  --data-raw '{
      "data": {
          "app": "X",
          "countryRegion": "EN"
      }
  }'
404 page not found
  1. What am I doing wrong?
  2. How to do it properly?

Upvotes: 0

Views: 1004

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28774

The multiline string from the parameter can be used as an argument to the Jenkins Pipeline shell step method, but the interpolation must occur within the pipeline as a Groovy variable, and not within the shell interpreter as a shell variable. Then the multiline strings will be delimited correctly by the shell interpreter. Changing the step to:

sh "curl ${params.CURL_OPTIONS}"

will cause this desired behavior.

Upvotes: 1

Related Questions