Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. begin
  2. require 'json'
  3. rescue LoadError => e
  4. require 'json/pure'
  5. end
  6.  
  7. module Rack
  8.  
  9. # A Rack middleware for parsing POST/PUT body data when Content-Type is
  10. # not one of the standard supported types, like <tt>application/json</tt>.
  11. #
  12. class PostBodyContentTypeParsers
  13.  
  14. # Constants
  15. #
  16. CONTENT_TYPE = 'CONTENT_TYPE'.freeze
  17. POST_BODY = 'rack.input'.freeze
  18. FORM_INPUT = 'rack.request.form_input'.freeze
  19. FORM_HASH = 'rack.request.form_hash'.freeze
  20.  
  21. # Supported Content-Types
  22. #
  23. APPLICATION_JSON = 'application/json'.freeze
  24.  
  25. def initialize(app)
  26. @app = app
  27. end
  28.  
  29. def call(env)
  30. case env[CONTENT_TYPE]
  31. when APPLICATION_JSON
  32. env.update(FORM_HASH => JSON.parse(env[POST_BODY].read), FORM_INPUT => env[POST_BODY])
  33. end
  34. @app.call(env)
  35. end
  36.  
  37. end
  38. end
Add Comment
Please, Sign In to add comment