ruby on rails - how to get pretty printing from a form-encoded string -


good sirs. how can force request.body (or other non-json string) print out in nice multi-lined json or yaml style?

i have seen fancy methods convert such strings real json hoping avoid putting in method.

  def request_token_from_google     uri = uri.parse('https://www.googleapis.com/oauth2/v3/token')     http = net::http.new(uri.host, uri.port)     http.use_ssl = true     http.verify_mode = openssl::ssl::verify_none     request = net::http::post.new(uri.request_uri)     request.set_form_data(self.to_params)     puts "request body is"     puts request.body.to_yaml # doesn't work     puts request.body.to_json # doesn't work     http.request(request)   end 

the problem this:

ap "request body #{request.body.to_json}" 

...and other attempts you're trying pretty-print that's string. purpose of awesome_print (and inspect , ilk) take object has structure , print such can see structure, string has no structure—it's character, character, character. when give awesome_print string "request body {"foo":... has no way of knowing there's special part after "is."

if had object structure, solution give directly awesome_print:

puts "request body is:" ap my_hash_or_array 

unfortunately, in case it's not going help, because request.body string, too—it's form-encoded data, (stolen wikipedia):

name=jonathan+doe&age=23&formula=a+%2b+b+%3d%3d+13%25%21 

just "request body is..." example, awesome_print has no way of knowing special. 1 simple thing put newline between each key/value pair:

body = "name=jonathan+doe&age=23&formula=a+%2b+b+%3d%3d+13%25%21"  puts "request body is:" puts body.gsub("&", "\n  &") # => request body is: #    name=jonathan+doe #      &age=23 #      &formula=a+%2b+b+%3d%3d+13%25%21 

this has downside values still percent-encoded, can see in case of formula. if that's problem can parse form data using cgi.parse or rack::utils.parse_query, both of available in rails. both return hash can give awesome_print, different formats (cgi returns values arrays, rack::utils if they're in "array" format, e.g. foo[]=1&foo[]=2). here's rack::utils (you'll have imagine output colored):

puts "request body is:" ap rack::utils.parse_query(body) # => request body is:" #    { #           "name" => "jonathan doe", #            "age" => "23", #        "formula" => "a + b == 13%!" #    } 

finally, little unsolicited advice: puts , ap, write stdout, tend work fine in development because rails' logger writing stdout, see puts output in same terminal window rails server log. in production, however, data written stdout might not written file anywhere, , if is, if change rails.logger configuration @ point, ap's output may still going somewhere else. want use rails' logger instead of puts or ap, can sure of output goes same place. fortunately, awesome_print adds awesome_inspect method every object, returns same pretty string see when use ap, can still use awesome_print rails.logger:

body_inspect = rack::utils.parse_query(body).awesome_inspect rails.logger.info("request body is:\n#{body_inspect}") 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -