使用httparty post方法时获取InvalidURIError并通过用户获取某些字段

时间:2017-08-06 13:54:13

标签: ruby-on-rails ruby ruby-on-rails-3

InvalidURIError(错误的URI(不是URI?):https://api.flightstats.com/flex/schedules/rest/v1/json/from/VNS/to/DEL/departing/2017/                        8/7 APPID = 94f56975&安培; APPKEY = 0a0dc2b64f177ab866f0dba59342ffa4) 如何解决这个错误。使用httparty post方法。

flight = HTTParty.post(" https://api.flightstats.com/flex/schedules/rest/v1/json/from/# {params [:from]} ​​/ to /#{params [:to]} / departure /#{params [:year]} /                        #{PARAMS [:月]} /#?{PARAMS [:天]} APPID = 94f56975&安培; APPKEY = 0a0dc2b64f177ab866f0dba59342ffa4&#34)

1 个答案:

答案 0 :(得分:0)

有两个问题。首先,网址包含#{param‌​s[:year]}#{params[:month]}之间的空格,这会导致无效的网址。代码尝试POST请求,但根据他们的documentation端点接受GET请求。

将您的代码更改为:

HTTParty.get(
  "https://api.flightstats.com/flex/schedules/rest/v1/json/from/"{params[:from]}/to/#{params[:to]}/departing/#{params[:year]}/#{params[:month]}/#{params[:day]}?appId=94f56975&appKey=0a0dc2b64f177ab866f0dba59342ffa4"
)