cURL cannot connect to localhost but browser can
Asked Answered
D

6

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.

Dimension answered 7/1, 2014 at 22:29 Comment(4)
can you try http://127.0.0.1:3000/api/data? Looks like for some reason curl goes straight to ipv6...Solingen
Thanks, that seems to work. Any idea why this is suddenly the case?Dimension
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.Solingen
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)Solingen
W
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.

Wesley answered 24/3, 2014 at 4:5 Comment(5)
Add a -4 to your command: curl -4 -H 'id:1' -i 'localhost:3000/api/data' -vBravissimo
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?Juliajulian
Another report of the IPv6 loopback causing an issue: superuser.com/a/831695/11889.Juliajulian
Is it possible to set it through curl_setopt in php?Hagar
damn I lost a couple of hours for this...many thanx for the hintConcertmaster
G
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']

Gosplan answered 28/10, 2014 at 13:48 Comment(1)
Why does it work with 0.0.0.0:3000 and not localhost:3000?Shon
Y
6

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

curl -x "" "http://127.0.0.1:3000"
Yseulta answered 21/1, 2016 at 14:54 Comment(0)
W
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
Walter answered 22/3, 2014 at 15:1 Comment(0)
B
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.

Britishism answered 30/9, 2015 at 9:47 Comment(0)
M
0

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

Merriweather answered 22/2, 2021 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.