-1

I had build a curl command

cmd = %Q(curl --tlsv1.2 -b #{config['cookies']} -c #{config['cookies']} --
connect-timeout 60 -X POST 
            -H 'Content-Type: application/xml'
            -H 'Accept: application/xml'
            -d '#{xml_content}'
            --location #{uri})
puts cmd

The output is as follow when the string is printed

curl --tlsv1.2 -b /tmp/cookie.txt -c /tmp/cookie.txt --connect-timeout 60 -X POST
            -H 'Content-Type: application/xml'
            -H 'Accept: application/xml'
            -d '<?xml version="1.0" encoding="UTF-8" ?>
            <ns:login xmlns:ns="http://www.actility.com/smp/ws/admin">
            <login>hello</login><password>wPa4GwYbRTCw0Uy!</password></ns:login>'
            --location https://myapi.application.com

How do I execute it in ruby w/o modifying the cmd string to a single cmd ?

Gavin Yap
  • 512
  • 7
  • 23

2 Answers2

1

I think that you should add to your cmd before the "\n" de "\" it should be scaped as follows:

[1] pry(main)> cmd = %Q(curl -i
[1] pry(main)* -H 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
[1] pry(main)* -XGET 'http://httpbin.org/basic-auth/user/password')
=> "curl -i\n" + "-H 'Authorization: Basic dXNlcjpwYXNzd29yZA=='\n" + "-XGET 'http://httpbin.org/basic-auth/user/password'"
[2] pry(main)> exec(cmd)
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
sh: line 1: -H: command not found
sh: line 2: -XGET: command not found

you should write this:

[1] pry(main)> cmd = %Q(curl -i \\
[1] pry(main)* -H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \\
[1] pry(main)* -XGET 'http://httpbin.org/basic-auth/user/password')
=> "curl -i \\\n" + "-H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \\\n" + "-XGET 'http://httpbin.org/basic-auth/user/password'"
[2] pry(main)> exec(cmd)
HTTP/1.1 200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Mon, 05 Jun 2017 09:46:24 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000503063201904
Content-Length: 47
Via: 1.1 vegur

{
  "authenticated": true,
  "user": "user"
}
anquegi
  • 9,627
  • 3
  • 41
  • 60
0

You already built a string containing the command, so you need to pass it to one of Ruby's shell execution methods. I'd suggest

result = %x(#{cmd})

Here is a more detailed answer you might find useful: https://stackoverflow.com/a/2400/2029766

knugie
  • 1,091
  • 8
  • 13
  • I got this error `curl: no URL specified! curl: try 'curl --help' or 'curl --manual' for more information sh: 2: -H: not found sh: 3: -H: not found sh: 4: -d: not found sh: 7: --location: not found` – Gavin Yap Jun 05 '17 at 09:48
  • I see the issue. A line break in string passed to `%x()` is just like hitting in the terminal. To prevent sending the command, you need to escape the newline character by prepending a '\' (as anquegi suggested) or you just remove the newlines for execution; `%x(#{cmd.tr("\n",'')})`. – knugie Jun 16 '17 at 21:19