2

Say I have the following curl call :

"curl  -v --insecure -X POST -H 'Host: api.test.com' -H 'Tester #{@@test_name}' -d 'token=#{token_raw}' https://teststage.BLAHBLAH:token/terminate"

I'm trying to get the output of the curl call. Such as the 200 OK or 404 ERROR status.

So, if I do :

a = `curl  -v --insecure -X POST -H 'Host: api.test.com' -H 'Tester #{@@test_name}' -d 'token=#{token_raw}' https://teststage.BLAHBLAH:token/terminate`

I get nothing back in a

However, if I do

puts `curl  -v --insecure -X POST -H 'Host: api.test.com' -H 'Tester #{@@test_name}' -d 'token=#{token_raw}' https://teststage.BLAHBLAH:token/terminate`

Then I can see the output. How do I read it into the variable. I'd prefer an answer without any import like OPEN3, if possible.

gran_profaci
  • 8,087
  • 15
  • 66
  • 99

3 Answers3

3

I'm not sure why

a = `curl  -v --insecure -X POST -H 'Host: api.test.com' -H 'Tester #{@@test_name}' -d 'token=#{token_raw}' https://teststage.BLAHBLAH:token/terminate`

isn't working for you.

Here's what I get with a simpler test:

RUBY_VERSION # => "1.9.3"

`curl --version`
# => "curl 7.30.0 (x86_64-apple-darwin13.0) libcurl/7.30.0 SecureTransport zlib/1.2.5\nProtocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp \nFeatures: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz \n"

a = `curl http://echo.jsontest.com/hello/world`

a # => "{\"hello\": \"world\"}\n"

UPDATE

I missed a part of your question and didn't realize you were looking for the headers.

Try this:

a = `curl -I http://echo.jsontest.com/hello/world`
a.lines.first # => "HTTP/1.1 200 OK\r\n"

Hopefully that helps.

Amiel Martin
  • 4,636
  • 1
  • 29
  • 28
1

You probably want to just use Ethon to interface with libcurl instead of calling out to the shell interface, especially if you're unwilling to use the tools Ruby gives you for interfacing with the shell and other processes more readily (you know, like Open3).

Note: the Ruby standard library (including Open3) is part of Ruby and distributed with it. It is in no sense an import, but if you really don't want to use the require method for some inane reason, IO is available without loading additional code and provides the low level interface that Open3 utilizes.

coreyward
  • 77,547
  • 20
  • 137
  • 166
1

Try this:

HTTP_RESP_CODE=$(curl -s -o out.html -w '%{http_code}' http://www.example.com)

echo $HTTP_RESP_CODE

This works with my ruby.

HTTP_RESP_CODE=`curl -s -o out.html -w '%{http_code}' http://www.example.com`
print HTTP_RESP_CODE
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85