为什么我的 API 请求使用 HTTParty 返回 400 响应?
Why is my API request returning a 400 response using HTTParty?
我正在创建一个简单的 Rails 应用程序,它从 Open Weather Map API 和 returns 表单中搜索的城市的当前天气数据中获取数据场地。我希望 API 调用看起来像这样,例如:
http://api.openweathermap.org/data/2.5/weather?q=berlin&APPID=111111
我已经在 Postman 中用我的 API 键测试了它,它工作正常但用我的代码它 returns "cod":"400","message":"Nothing to geocode"
谁能看出我错在哪里?这是我的代码。
services/open_weather_api.rb
class OpenWeatherApi
include HTTParty
base_uri "http://api.openweathermap.org"
def initialize(city = "Berlin,DE", appid = "111111")
@options = { query: { q: city, APPID: appid } }
end
def my_location_forecast
self.class.get("/data/2.5/weather", @options)
end
end
forecasts_controller.rb
class ForecastsController < ApplicationController
def current_weather
@forecast = OpenWeatherApi.new(@options).my_location_forecast
end
end
current_weather.html.erb
<%= form_tag(current_weather_forecasts_path, method: :get) do %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %><br>
<%= @forecast %>
routes.rb
Rails.application.routes.draw do
root 'forecasts#current_weather'
resources :forecasts do
collection do
get :current_weather
end
end
end
错误描述本身:
"cod":"400","message":"Nothing to geocode"
这意味着您没有在查询中提供城市。此错误的一个可能原因是您在 initialize
方法中使用来自控制器的 @options
变量覆盖此行中的默认值:
class ForecastsController < ApplicationController
def current_weather
@forecast = OpenWeatherApi.new(@options).my_location_forecast
end
end
根据您提供的信息,您没有在控制器中定义 @options
变量,或者它是 nil
。因此,这将覆盖 OpenWeatherApi
中 initialize
方法的默认值。
由于你的appid不会改变,只有城市名称会改变,所以你可以从控制器发送它。
def current_weather
@city = params[:city] // the city you want to send to API. Change it with your value
@forecast = OpenWeatherApi.new(@city).my_location_forecast
end
我正在创建一个简单的 Rails 应用程序,它从 Open Weather Map API 和 returns 表单中搜索的城市的当前天气数据中获取数据场地。我希望 API 调用看起来像这样,例如:
http://api.openweathermap.org/data/2.5/weather?q=berlin&APPID=111111
我已经在 Postman 中用我的 API 键测试了它,它工作正常但用我的代码它 returns "cod":"400","message":"Nothing to geocode"
谁能看出我错在哪里?这是我的代码。
services/open_weather_api.rb
class OpenWeatherApi
include HTTParty
base_uri "http://api.openweathermap.org"
def initialize(city = "Berlin,DE", appid = "111111")
@options = { query: { q: city, APPID: appid } }
end
def my_location_forecast
self.class.get("/data/2.5/weather", @options)
end
end
forecasts_controller.rb
class ForecastsController < ApplicationController
def current_weather
@forecast = OpenWeatherApi.new(@options).my_location_forecast
end
end
current_weather.html.erb
<%= form_tag(current_weather_forecasts_path, method: :get) do %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %><br>
<%= @forecast %>
routes.rb
Rails.application.routes.draw do
root 'forecasts#current_weather'
resources :forecasts do
collection do
get :current_weather
end
end
end
错误描述本身:
"cod":"400","message":"Nothing to geocode"
这意味着您没有在查询中提供城市。此错误的一个可能原因是您在 initialize
方法中使用来自控制器的 @options
变量覆盖此行中的默认值:
class ForecastsController < ApplicationController
def current_weather
@forecast = OpenWeatherApi.new(@options).my_location_forecast
end
end
根据您提供的信息,您没有在控制器中定义 @options
变量,或者它是 nil
。因此,这将覆盖 OpenWeatherApi
中 initialize
方法的默认值。
由于你的appid不会改变,只有城市名称会改变,所以你可以从控制器发送它。
def current_weather
@city = params[:city] // the city you want to send to API. Change it with your value
@forecast = OpenWeatherApi.new(@city).my_location_forecast
end