1

I'm working with a test in which I need to hit a service I'm running locally. The service is a rails application with a JSON api exposed. It's running on port 3000. When I execute this elixir code I get an error:

json_event = <some valid json built earlier>
my_url = "http://localhost:3000/api/service_call"
response = HTTPoison.post(my_url, json_event)

The error I get is:

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

I should note that there is no output found in the service log when I execute that code.

my question is can the port be specified, and if so have I done so correctly by simply building it into the url?

I guess a follow on question would be what are the reasons that I might get this :econnrefused error?

I'm not even sure I'm asking the RIGHT questions :P

thanks

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • Is this code running on the same server where said localhost service exists? the url seems fine. – Kevin B Nov 13 '17 at 17:13
  • yes indeed :) I should also clarify that I'm running it from a ExUnit test – jaydel Nov 13 '17 at 17:14
  • 1
    Your Elixir code seems fine. `econnrefused` indicates that your rails server did not start. Double check your rails server with a `curl` command if need be. – Kevin Johnson Nov 13 '17 at 17:15
  • okay, I'll go down that path. thanks – jaydel Nov 13 '17 at 17:16
  • okay, I'm able to get to my server with curl. not sure if I'm happy about that or sad :) – jaydel Nov 13 '17 at 17:31
  • Could you try example provided on github: `HTTPoison.post "http://httparrot.herokuapp.com/post", "{\"body\": \"test\"}", [{"Content-Type", "application/json"}]`. If that works, then can you sanity check `cat /etc/hosts` that you have the line `127.0.0.1 localhost` present? – Kevin Johnson Nov 13 '17 at 18:58
  • so I am mortified to admit that once I rebooted my entire work environment, things fell into place. At this point things are working but I have no idea what changed :/ – jaydel Nov 15 '17 at 12:36

2 Answers2

2

We've seen this on multiple machines.

We originally got around it by using ngrok.

Then we found this comment suggesting it was an ipv6 issues, followed the steps listed and it worked for us. https://github.com/scrogson/oauth2/issues/94#issuecomment-308665656

ignu
  • 158
  • 10
0

So two parts to the answer here:

  1. Yes, you can specify port, and you can do it by simply putting it into the URL itself. For example, http://localhost:3000/api/service_call.

  2. rebooting fixed whatever was wrong with my system. I have no idea why it made a difference.

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • Specifying the port is a URL feature. It has nothing to do with any library you may be using. Check RFC standard: https://tools.ietf.org/html/rfc3986#section-3.2.3 – Kevin Johnson Nov 15 '17 at 18:05
  • That is exactly what I expected to be true but it wasn't working on my system. I had the target server running locally on port 3000 and it was receiving no messages. I definitely know that it SHOULD have worked. However, it was fine once I rebooted my system. – jaydel Nov 22 '17 at 14:43