简体   繁体   中英

Httpoison request to elasticsearch server gives me econnrefused error, while curling the same thing doesn't

When I try to use httpoison to query an elasticsearch server like

iex(1)> HTTPoison.get "http://localhost:9200"

I get

{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}.

If I do

curl -XGET "http://localhost:9200"

I get

{
  "name" : "es01",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "Wik-EpMkQ8ummJE6ctNAOg",
  "version" : {
    "number" : "7.0.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "e4efcb5",
    "build_date" : "2019-04-29T12:56:03.145736Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Does anyone know what this behavior is due to and how to fix it?

PS: Changing localhost to 127.0.0.1 does not solve the problem.

Here's my setup:

elasticsearch Version: 7.0.1
{:httpoison, "~> 1.5"}  #=> mix.lock shows version 1.5.1 was installed

curl results :

$ curl -XGET "http://localhost:9200"
{
  "name" : "My-MacBook-Pro.local",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "vEFl3B5TTYaBxPhQFuXPyQ",
  "version" : {
    "number" : "7.0.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "e4efcb5",
    "build_date" : "2019-04-29T12:56:03.145736Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

HTTPoison results:

$ iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 3 files (.ex)
Generated httpoison app
==> hello
Compiling 15 files (.ex)
Generated hello app
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> HTTPoison.get "http://localhost:9200"
{:ok,
 %HTTPoison.Response{
   body: "{\n  \"name\" : \"My-MacBook-Pro.local\",\n  \"cluster_name\" :  
\"elasticsearch\",\n  \"cluster_uuid\" : \"vEFl3B5TTYaBxPhQFuXPyQ\",\n  
\"version\" : {\n    \"number\" : \"7.0.1\",\n    \"build_flavor\" :   
\"default\",\n    \"build_type\" : \"tar\",\n    \"build_hash\" :   
\"e4efcb5\",\n    \"build_date\" : \"2019-04-29T12:56:03.145736Z\",\n  
\"build_snapshot\" : false,\n    \"lucene_version\" : \"8.0.0\",\n  
\"minimum_wire_compatibility_version\" : \"6.7.0\",\n  
\"minimum_index_compatibility_version\" : \"6.0.0-beta1\"\n  },\n  
\"tagline\" : \"You Know, for Search\"\n}\n",
   headers: [
     {"content-type", "application/json; charset=UTF-8"},
     {"content-length", "522"}
   ],
   request: %HTTPoison.Request{ 
     body: "",
     headers: [],
     method: :get,
     options: [],
     params: %{},
     url: "http://localhost:9200"
   },
   request_url: "http://localhost:9200",
   status_code: 200
 }}

iex(2)> 

Next, I stopped the elasticsearch server, then I ran the HTTPoison request again:

ex(2)> HTTPoison.get "http://localhost:9200"
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}

I got similar results for the curl request:

$ curl -XGET "http://localhost:9200"
curl: (7) Failed to connect to localhost port 9200: Connection refused

What happens if you issue two curl requests in a row? Do they both succeed? Try issuing the HTTPoison request first, then the curl request. Which one fails? Try the reverse order. Same results?

I'm almost positive you have the same problem that I did. Check to make sure you are not forcing ipv6 in your /etc/hosts file.

If you have something like this:

::1 localhost

...get rid of it and Httpoison should work again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM