Say I have the following curl
call :
假设我有下面的curl调用:
"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.
我试图得到旋度调用的输出。例如200 OK或404错误状态。
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
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.
然后我可以看到输出。如何把它读入变量。如果可能的话,我想要一个不像OPEN3那样的输入的答案。
3 个解决方案
#1
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.
希望有帮助。
#2
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).
您可能只想使用Ethon来与libcurl进行交互,而不是调用shell接口,特别是如果您不愿意使用Ruby提供的工具来更容易地与shell和其他进程进行交互(您知道,比如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.
注意:Ruby标准库(包括Open3)是Ruby的一部分,并与之一起发布。它在某种意义上并不是一个导入,但是如果您确实不希望出于某种愚蠢的原因使用require方法,那么IO就可以在不加载额外代码的情况下使用,并提供Open3使用的低级别接口。
#3
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
.
这是我的ruby。
HTTP_RESP_CODE=`curl -s -o out.html -w '%{http_code}' http://www.example.com`
print HTTP_RESP_CODE
#1
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.
希望有帮助。
#2
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).
您可能只想使用Ethon来与libcurl进行交互,而不是调用shell接口,特别是如果您不愿意使用Ruby提供的工具来更容易地与shell和其他进程进行交互(您知道,比如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.
注意:Ruby标准库(包括Open3)是Ruby的一部分,并与之一起发布。它在某种意义上并不是一个导入,但是如果您确实不希望出于某种愚蠢的原因使用require方法,那么IO就可以在不加载额外代码的情况下使用,并提供Open3使用的低级别接口。
#3
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
.
这是我的ruby。
HTTP_RESP_CODE=`curl -s -o out.html -w '%{http_code}' http://www.example.com`
print HTTP_RESP_CODE