title: Creating command line strings with groovy for cURL - cURL ignores options
body: <p>I need help figuring out why the last two parameters of my cURL query are ignored. </p>
  
  <p>Please refrain on comment on how this is not the best way to do a rest call. I KNOW. This is going to be a kind of fall back method / work around for another issue. 
  I manyl handle my rest-work with the wslite (1.1.2) API. </p>
  
  <p>Now let me explain what i do: 
  I am using the groovy shell executor to make a command line call for a rest service via cURL. </p>
  
  <p>I have built a little class to build the query string and handle the command line: </p>
  
  <pre><code>    class Curl {
  
      def static getUserLogin(){
          def url                 = '"https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser '
          def requestFilePath     = '-d @temp/LoginPayload.json '
          def heads               = "-H 'Content-Type: application/json' -H 'Accept: text/plain' " 
          def params              = '-k -v' //-k = ignore unsecure -v = more verbose output
          def fullurl             = url+requestFilePath+heads+params
          return ex(fullurl)
      }
  
      /**
      * 
      * @param _command The command you want to execute on your shell. 
      * @param _workingDir Optional: You may specify the directory where the command will be executed. Default is user dir.
      * @return Exit value for the process. 0 = normal termination.
      */
    def static ex(String _command, File _workingDir = new File(System.properties.'user.dir')) {
      println "Executing command&gt; $_command \n"
      def process = new ProcessBuilder(addShellPrefix(_command))
                                        .directory(_workingDir)
                                        .redirectErrorStream(true)
                                        .start()
      process.inputStream.eachLine {println it}
      process.waitFor();
      return process.exitValue().value
    }
  
    private static addShellPrefix(String _command) {
      def commandArray = new String[2]
      commandArray[0] = "curl "
      commandArray[1] = _command
      return commandArray
    }
  }
  
  
  Curl.getUserLogin() //to execute
  </code></pre>
  
  <p>I hope the code is self-explenatory enough. It all works fine with simple URLs respectively with less parameters. </p>
  
  <p>Executing this will yield the following response (excerpt from the full debug output):</p>
  
  <blockquote>
    <p>Executing command>
    "<a href="https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser" rel="nofollow">https://some-login.someSystem-dev.someHost.com/someResource.beyond.foobar/login/LoginAUser</a>"
    -d @temp/LoginPayload.json -H 'Content-Type: application/json' -H 'Accept: text/plain' -k -v </p>
    
    <p>% Total    % Received % Xferd  Average Speed   Time    Time     Time
    Current
                                     Dload  Upload   Total   Spent    Left  Speed</p>
    
    <p>0     0    0     0    0     0      0      0 --:--:-- --:--:--
    --:--:--     0   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0 curl: (60) SSL certificate problem: self signed certificate in certificate chain More details here:
    <a href="http://curl.haxx.se/docs/sslcerts.html" rel="nofollow">http://curl.haxx.se/docs/sslcerts.html</a></p>
    
    <p>curl performs SSL certificate verification by default, using a
    "bundle"  of Certificate Authority (CA) public keys (CA certs). If the
    default  bundle file isn't adequate, you can specify an alternate file
    using the --cacert option. If this HTTPS server uses a certificate
    signed by a CA represented in  the bundle, the certificate
    verification probably failed due to a  problem with the certificate
    (it might be expired, or the name might  not match the domain name in
    the URL). If you'd like to turn off curl's verification of the
    certificate, use  the -k (or --insecure) option.</p>
  </blockquote>
  
  <p>Now, as you can see I have attached the required option "-k" to the query string but somehow it is ignored. Using this string directly in the windows command line tool (if you try this make sure you escape potential double quotes) works perfectly fine though. </p>
  
  <p>Any ideas why this happens or how I could accquire more debug information? </p>
  
  <p>Thx in advance! </p>
  
  <p>UPDATE:
  Solution:<br>
  Passing ever option as a single argument (via a list) fixed the issue. </p>
  
  <p>New Issue: 
  After that i wante curl to output the response to a file using '-o C:\Temp\response.txt' to the argument list. This works fine when used from a command line tool. Executing it from the groovy script results in: </p>
  
  <blockquote>
    <p>curl: (23) Failed writing body (0 != 386)</p>
  </blockquote>
  
  <p>I can get around this by just writing the stream to a file. What is really bugging me is that fact that the response does not seem to contain any information in the body. Executing the curl command from windows command line tool returns me a pretty long token as expected.  </p>
  
  <p>Andy ideas? </p>