cURL无法连接到localhost但浏览器可以

jbe*_*eck 29 curl ruby-on-rails thin osx-mavericks

我在本地运行Rails应用程序(瘦服务器),我可以从浏览器本地连接(localhost:3000),但是当我尝试使用curl时,我得到:

curl -H 'id:1' -i 'http://localhost:3000/api/data' -v

* Hostname was NOT found in DNS cache
*   Trying ::1...
* Adding handle: conn: 0x7fd121808200
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fd121808200) send_pipe: 1, recv_pipe: 0
* Connection failed
* connect to ::1 port 3000 failed: Connection refused
*   Trying fe80::1...
* Connection failed
* connect to fe80::1 port 3000 failed: Connection refused
* Failed to connect to localhost port 3000: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 3000: Connection refused
Run Code Online (Sandbox Code Playgroud)

这曾经工作得很好,但我最近更新到小牛队,我怀疑可能已经破坏了一些东西.我也可以从网上成功卷曲.

Ste*_*lay 53

这是一个卷曲错误(一个奇怪的错误),如果/ etc/hosts中的IPv6条目没有响应,则curl无法回退到IPv4.

您可以通过该-4选项强制它使用IPv4 .

  • 在你的命令中加一个-4:curl -4 -H'id:1'-i'http:// localhost:3000/api/data'-v (11认同)

Mar*_*ins 22

代替

curl localhost:3000
Run Code Online (Sandbox Code Playgroud)

尝试

curl 0.0.0.0:3000
Run Code Online (Sandbox Code Playgroud)

有用.
如果没有,请在启动rails服务器时检查输出:

=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-10-28 15:30:08] INFO  WEBrick 1.3.1
[2014-10-28 15:30:08] INFO  ruby 2.0.0 (2014-02-24) [x86_64-darwin12.5.0]
[2014-10-28 15:30:08] INFO  WEBrick::HTTPServer#start: pid=4004 port=3000
Run Code Online (Sandbox Code Playgroud)

使用第2行末尾的url而不是 ENV['HOST']

  • 为什么它适用于`0.0.0.0:3000`而不是`localhost:3000`? (4认同)

Pit*_*kos 5

你在代理后面吗?然后使用下面的完全绕过代理。

curl -x "" "http://127.0.0.1:3000"
Run Code Online (Sandbox Code Playgroud)


Fre*_*man 1

假设您没有触及 /etc/hosts 并且 scutil 报告是肯定的:

$ scutil -r 127.0.0.1
Reachable,Local Address
$ scutil -r localhost
Reachable,Local Address
Run Code Online (Sandbox Code Playgroud)

那么我的猜测是防火墙处于活动状态并且不接受来自curl 的连接。

尝试将curl添加到接受的应用程序列表中(我猜测语言替代方案,因为我的机器设置为使用瑞典语)首选项-->安全-->防火墙替代方案-->加号-->(搜索curl和添加它)

注意:确保添加您在 shell 中实际使用的卷曲。

$ type -a curl
curl is /opt/local/bin/curl
curl is /usr/bin/curl
Run Code Online (Sandbox Code Playgroud)