API 端点 link 在 Postman 中工作,但在 Rails 上的 Ruby 中没有使用 ActiveResource

API endpoint link is working in Postman but not in Ruby on Rails with ActiveResource

我正在 Ruby 开发 Rails 应用程序,该应用程序需要通过 GET 请求访问外部 API。请求需要随请求发送一些参数。我使用 Activeresource gem 访问 API。 API 请求在 Postman 中运行良好,并给出了预期的记录,但 Rails 应用程序给我一个 400 错误。

代码如下

class PayRunEmploymentHeroBase < ActiveResource::Base
  self.site = "https://api.yourpayroll.com.au"
  self.user = ENV['EMPLOYMENT_HERO_USERNAME']
  self.element_name = ""
  self.include_format_in_path = false
  self.prefix = "/api/v2/business/:business_id/payrun?$filter=DateFinalised gt datetime':finalised_date'"
end

我在此处粘贴了从 Rails 应用程序访问 link 时出现 400 错误的详细信息。

Reloading...
 => true
2.3.0 :006 > PayRunEmploymentHeroBase.find(:all, params: {:business_id => business_id, :finalised_date =>  finalised_date})
##############################################INFO##############################################
{
  "method": "get",
  "request_uri": "https://api.yourpayroll.com.au:443/api/v2/business/48058/payrun?$filter=DateFinalised gt datetime'2017-11-16'",
  "http_status": "400",
  "http_message": "Bad Request"
}
##############################################INFO##############################################
ActiveResource::BadRequest: Failed.  Response code = 400.  Response message = Bad Request.
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:141:in `handle_response'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:126:in `request'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:85:in `block in get'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:220:in `with_auth'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/connection.rb:85:in `get'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/base.rb:1073:in `find_every'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activeresource-5.1.0/lib/active_resource/base.rb:974:in `find'
    from (irb):6
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/console.rb:65:in `start'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<main>'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `require'
    from /Users/bebble1/work/awe/bin/rails:9:in `<main>'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `load'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/bootsnap-1.0.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `load'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `block in load'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/bebble1/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `load'
    from /Users/bebble1/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/bebble1/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in `require'

我还附上了 Rails app

结果的图片

您似乎没有使用 Rails 应用程序进行身份验证。我确定您已经在 Postman 中设置了身份验证 username/password。

self.password = "password"(使用您自己的密码)设置为 PayRunEmploymentHeroBase 即可。

更新: 其实是你的url没有编码,这样做就可以解决问题:

self.prefix = "/api/v2/business/:business_id/payrun?$filter=" + URI::encode("DateFinalised gt datetime':finalised_date'")