Failed to open TCP connection to :80 HTTParty gem

Mihir Deshpande picture Mihir Deshpande · Jan 5, 2018 · Viewed 9.1k times · Source

So in my course on Coursera I'm required to build this rather simple aplication to obtain and display an array from an external api. I'm using the ruby on rails framework. (I'm using windows 10)

Controller -

class CoursesController < ApplicationController
  def index
    @search_term = params[:looking_for] || 'jhu'
    @courses = Coursera.for(@search_term)
  end
end

Model

class Coursera 
    include HTTParty
    default_options.update(verify: false) # Turn off SSL verification
    base_uri 'https://api.coursera.org/api/courses.v1'
    default_params fields: "photoUrl,description",q: "search"
    format :json

    def self.for term
        get("",query: {query: term}) ["elements"]
    end
end

The view is irrelevant as this works fine. But in my other app , I get this error -

Errno::ECONNREFUSED: Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80)

This is the other app which I'm having the problem with -

Controller -

class RecipesController < ApplicationController
  def index
    @search_term = params[:search] || 'chocolate'
    @recipes = Recipe.for @search_term

  end

end

Model -

class Recipe
    include HTTParty
    default_options.update(verify: false) # Turn off SSL verification
    key_value = ENV['FOOD2FORK_KEY']
    hostport = ENV['FOOD2FORK_SERVER_AND_PORT'] || 'food2fork.com'
    base_uri = "https://doesntmatter.com/api"  #website mentioned here         
                               #doesn't matter , I get the error nonetheless
    default_params key: ENV['FOOD2FORK_KEY'] ,q: "search"
    format :json
    def self.for term
        get("/search",query: {query: term}) ["recipes"]
    end

end

I've tried disabling all possible firewalls , unblocked all ports with TCP in windows firewall , but I still get the same error. Any ideas how to fix this ? Because I don't think its a problem in my code yet...

Answer

Зелёный picture Зелёный · Jan 5, 2018

You have an excess symbol in your code:

class Recipe
  # some code here
  base_uri = "https://doesntmatter.com/api" 
           ^ # the equal symbol is redundant.
             # remove it and all will works as expected
  # ....
end