I am working on Ruby on Rails application which requires to access an External API via GET requests. The request needs to send some parameters with request. I have used Activeresource gem for accessing API. The API request is working fine in Postman and giving expected records but gives me a 400 error from Rails application.
Following is the code
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
I am pasting here the details of 400 error while accessing the link from Rails app.
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'
I have also attached a picture here of the outcome from Rails app
It seems like you are not authenticating with Rails app. And I am sure you have set authentication username/password in Postman.
Set self.password = "password"
(use your own password) to PayRunEmploymentHeroBase
and it will work.
Update: It's actually your url is not encoded, do this and it will solve the issue:
self.prefix = "/api/v2/business/:business_id/payrun?$filter=" + URI::encode("DateFinalised gt datetime':finalised_date'")