32

I'm running a Rails app locally (Thin server), and I can connect locally from the browser (localhost:3000), but when I try to use curl, I get:

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

This used to work just fine, but I recently updated to Mavericks, which I suspect may have broken something. I can also curl successfully from the web.

jbeck
  • 2,184
  • 1
  • 19
  • 21
  • 5
    can you try `http://127.0.0.1:3000/api/data`? Looks like for some reason curl goes straight to ipv6... – fvu Jan 07 '14 at 22:32
  • Thanks, that seems to work. Any idea why this is suddenly the case? – jbeck Jan 07 '14 at 22:35
  • I'm not a mac-person, but I think the entry for localhost may have disappeared from /etc/hosts (there should be a line like `127.0.0.1 localhost` to link the name localhost to the loopback address 127.0.0.1. If that line is there, there may be a problem with resolv.conf or whatever confirguration file a mac uses to decide whether to consult the hostsfile and/or dns, and the order in which it does. – fvu Jan 07 '14 at 22:42
  • Have look here http://apple.stackexchange.com/questions/107840/cannot-access-localhost-after-upgrade-to-mavericks-but-can-access-127-0-0-1 , looks like running `dscacheutil -flushcache` could be what you need to do (assuming /etc/hosts is OK) – fvu Jan 07 '14 at 22:47

6 Answers6

56

This is a curl bug (a strange one), where curl fails to fall back to IPv4 if there's an IPv6 entry in /etc/hosts that doesn't respond.

You can force it to use IPv4 via the -4 option.

Steve Clay
  • 8,671
  • 2
  • 42
  • 48
22

Instead of

curl localhost:3000

try

curl 0.0.0.0:3000

It works.
If it doesn't, check the output when you start your rails server:

=> 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

Use the url at the end of line 2 instead of ENV['HOST']

Marco Prins
  • 7,189
  • 11
  • 41
  • 76
6

Are you behind a proxy? Then use the below to bypass the proxy altogether.

curl -x "" "http://127.0.0.1:3000"
Pithikos
  • 18,827
  • 15
  • 113
  • 136
1

Assuming that you have not touched /etc/hosts and scutil reports are positive:

$ scutil -r 127.0.0.1
Reachable,Local Address
$ scutil -r localhost
Reachable,Local Address

then my guess is that the Firewall is active and not accepting connections from curl.

Try adding curl to the list accepted applications under (I'm guessing at the language alternatives since my machine is set to use Swedish) Preference --> Security --> Firewall alternatives --> plus sign --> (serach for curl and add it)

Note: make sure that you add the curl that you are actually using in your shell.

$ type -a curl
curl is /opt/local/bin/curl
curl is /usr/bin/curl
FredrikHedman
  • 1,223
  • 7
  • 14
0

Another cause of this issue can be a poorly configured system with a proxy address. Run

export http_proxy=

and rerun the curl command, to eliminate this as a contributing factor. It fixed this issue for me.

Philluminati
  • 2,649
  • 2
  • 25
  • 32
0

Restarting the computer can sometimes fix odd connection issues. I'm not sure why.

Bryan Aneux
  • 328
  • 2
  • 9