[英]didn't get a specific answer
i'm working with ruby and consuming an api called food2fork.com where i have two files recipes_controller.rb
and recipes.rb
我正在使用 ruby 并使用一个名为food2fork.com的 api,其中我有两个文件recipes_controller.rb
和recipes.rb
#recipes.rb
require 'httparty'
class Recipe
include HTTParty
default_options.update(verify: false)
base_uri "http://food2fork.com/api"
default_params key: ENV["FOOD2FORK_KEY"]
format :json
def self.for (para)
get("/search",query: {q:para})["recipes"]
end
end
and the second file recipes_controller.rb
和第二个文件recipes_controller.rb
require_relative 'recipes'
puts Recipe.for("chocolate")
but when i run recipes_controller.rb
file in command line.i get an error但是当我在命令行中运行recipes_controller.rb
文件时,出现错误
`
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>'
It seems like you are sending wrong api key好像您发送了错误的 api 密钥
Try to do it like this:尝试这样做:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.