Ruby on rails 没有得到具体的答案
我正在使用ruby并使用一个名为food2fork.com的api 我有两个文件 recipes_controller.rb和recipes.rb 第二个文件是recipes_controller.rb 但当我在命令行中运行recipes_controller.rb文件时,我得到了一个错误Ruby on rails 没有得到具体的答案,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用ruby并使用一个名为food2fork.com的api 我有两个文件 recipes_controller.rb和recipes.rb 第二个文件是recipes_controller.rb 但当我在命令行中运行recipes_controller.rb文件时,我得到了一个错误 ` C:\Users\Hamza\module-3-assignment-1>ruby recipes_controller.rb C:/RailsInstaller/Ruby2.3.3
`
C:\Users\Hamza\module-3-assignment-1>ruby recipes_controller.rb
C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/json/common.rb:156:in `parse':
822: unexpected token at 'FORBIDDEN' (JSON::ParserError)
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/json/common.rb:156:in `
parse'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:125:in `json'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:145:in `parse_supported_format'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:110:in `parse'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:69:in `call'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/request.rb:391:in `parse_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/request.rb:359:in `block in handle_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/response.rb:25:in `parsed_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/response.rb:96:in `method_missing'
from C:/Users/Hamza/module-3-assignment-1/receipes.rb:12:in `for'
from recipes_controller.rb:4:in `<main>'
似乎您发送了错误的api密钥 试着这样做:
class Recipe
include HTTParty
key_value = ENV['FOOD2FORK_KEY'] || 'YOUR_API_KEY_HERE'
base_uri "http://food2fork.com/api"
default_params key: key_value
default_options.update(verify: false)
format :json
def self.for(search_string)
get('/search', { query: { q: search_string}})["recipes"]
end
end
似乎响应不是JSON。检查是否可以使用提供的密钥手动访问URL。此外,ENV[a4211b7526158c71df283b3b5097eb36]末端的空间是否也是有意的?否则,这可能会导致将nil值作为键传递给请求。当我尝试手动访问它时,它返回403禁止是的,它是这样工作的。您能告诉我出了什么问题吗,因为在此之前,我还使用了提供给我的正确api键。当我注册时??如果字符串ENV['FOOD2FORK\u key',则我的脚本设置默认键if ENV key为空,如果字符串ENV['FOOD2FORK\u key']| |“YOUR_API_KEY_HERE”可以工作,但string ENV['FOOD2FORK_KEY']不能工作。这意味着您的ENV中没有FOOD2FORK_KEY,因为您必须在启动脚本之前在环境中设置它。简单例子
`
C:\Users\Hamza\module-3-assignment-1>ruby recipes_controller.rb
C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/json/common.rb:156:in `parse':
822: unexpected token at 'FORBIDDEN' (JSON::ParserError)
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/2.3.0/json/common.rb:156:in `
parse'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:125:in `json'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:145:in `parse_supported_format'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:110:in `parse'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/parser.rb:69:in `call'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/request.rb:391:in `parse_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/request.rb:359:in `block in handle_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/response.rb:25:in `parsed_response'
from C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/httparty-
0.16.2/lib/httparty/response.rb:96:in `method_missing'
from C:/Users/Hamza/module-3-assignment-1/receipes.rb:12:in `for'
from recipes_controller.rb:4:in `<main>'
class Recipe
include HTTParty
key_value = ENV['FOOD2FORK_KEY'] || 'YOUR_API_KEY_HERE'
base_uri "http://food2fork.com/api"
default_params key: key_value
default_options.update(verify: false)
format :json
def self.for(search_string)
get('/search', { query: { q: search_string}})["recipes"]
end
end