0

This feels like it should be simple, but after Googling for an hour I can't figure this out.

I'm POSTing an Amazon S3 'policy document' as JSON to my server. I need to encode the JSON as is, but Rails is adding stuff to 'params' which is cluttering the JSON I need to encode.

Here is what I have:

class Api::Amazons3Controller < Api::BaseController

 def sign_policy
   policy_document = params.except(:action, :controller)
   encoded_policy_document = Base64.encode64(policy_document.to_json).gsub(/\n|\r/, '')
   signature = Base64.encode64(
     OpenSSL::HMAC.digest(
       OpenSSL::Digest::Digest.new('sha1'), 
       ENV['AWS_SECRET_ACCESS_KEY'], 
       policy_document)
   ).gsub(/\n/, '')

   response = { policy: policy_document, signature: signature }
   render json: response
 end
end

I'm trying to 'clean up' the params with params.except(:action, :controller), but policy_document.to_json adds a root note called 'amazons3' (the controller name) around the JSON, which I don't want. I just need to encode the pure json from the request.

Any help would be highly appreciated!

502502
  • 437
  • 1
  • 5
  • 15
  • 1
    Your answer lies here [http://stackoverflow.com/questions/6515436/rails-3-1-include-root-in-json][1] [1]: http://stackoverflow.com/questions/6515436/rails-3-1-include-root-in-json – techvineet Aug 28 '13 at 02:36
  • Thank you for pointing me to wrap_parameters, if I change that it works, but I need wrap_parameters everywhere else in my app.. just not in THIS particular method. Anything I can do about that? – 502502 Aug 28 '13 at 02:51

3 Answers3

0
class Api::Amazons3Controller < Api::BaseController
    self.include_root_in_json = false
end
wedens
  • 1,782
  • 14
  • 18
  • This throws an exception: ActionController::RoutingError (undefined method 'include_root_in_json=' for Api::Amazons3Controller:Class) – 502502 Aug 28 '13 at 02:40
0

Try this then

config/initializers/wrap_parameters.rb

if defined?(ActiveRecord)
  ActiveRecord::Base.include_root_in_json = false
end
techvineet
  • 5,041
  • 2
  • 30
  • 28
0

I was able to disable the Parameter Wrapping for this Controller by adding:

class Api::Amazons3Controller < Api::BaseController
 wrap_parameters format: []
502502
  • 437
  • 1
  • 5
  • 15