33

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.

4
  • 5
    can you try http://127.0.0.1:3000/api/data? Looks like for some reason curl goes straight to ipv6...
    – fvu
    Commented Jan 7, 2014 at 22:32
  • Thanks, that seems to work. Any idea why this is suddenly the case?
    – jbeck
    Commented Jan 7, 2014 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
    Commented Jan 7, 2014 at 22:42
  • Have look here apple.stackexchange.com/questions/107840/… , looks like running dscacheutil -flushcache could be what you need to do (assuming /etc/hosts is OK)
    – fvu
    Commented Jan 7, 2014 at 22:47

6 Answers 6

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.

5
  • 12
    Add a -4 to your command: curl -4 -H 'id:1' -i 'localhost:3000/api/data' -v
    – Kylar
    Commented Nov 12, 2014 at 6:07
  • It does seem to be an IPv6 issue. If I comment out the "fe80::1%lo0 localhost" from my /etc/hosts file, curl works fine. I have two macs, both Yosemite, yet only one of them has this entry. Can anyone comment on why one host would have an IPv6 entry for localhost? Commented Feb 17, 2015 at 23:03
  • Another report of the IPv6 loopback causing an issue: superuser.com/a/831695/11889. Commented Feb 17, 2015 at 23:09
  • Is it possible to set it through curl_setopt in php?
    – Gino Pane
    Commented Nov 7, 2016 at 18:25
  • damn I lost a couple of hours for this...many thanx for the hint
    – pkaramol
    Commented Mar 27, 2019 at 16:26
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']

1
  • 5
    Why does it work with 0.0.0.0:3000 and not localhost:3000?
    – allegutta
    Commented Jan 19, 2015 at 19:54
6

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

curl -x "" "http://127.0.0.1:3000"
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
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.

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.