parse URI object, and fetch response from it, in one step


/ Published in: Other
Save to your folder(s)

URI objects can be opened directly by open-uri


Copy this code and paste it in your HTML
  1. require "open-uri"
  2.  
  3. uri="http://www.ruby-lang.org/en/"
  4.  
  5. html=URI.parse(uri).read # string representation is the response body
  6.  
  7. http_headers=URI.parse(uri).read.meta # this hash's keys are also accessible as OpenURI instance methods, e.g. html.content_type

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.