Archive

Posts Tagged ‘http’

Ruby: Using URI and HTTPClient

February 25, 2015 Leave a comment

HTTPClient provides API library for user to access web resource using HTTP

This would be helpful if you need to access a different web URL other than your base URL that you used in your automation and validate the content of the page.

Here’s a snippet of how to use HTTPClient in this particular scenario


require “httpclient”

my_url = “http://CheckThisOut:8080”

##method to access my_url using URI

def accessPage

begin

uri_parsed = URI.parse(my_url)

client = HTTPClient.new()

client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE

client.no_proxy = uri_parsed.host

uri = my_url

#invoke url to take the page content

puts “Accessing: #{uri}”

resp = client.get uri, :header => {’Accept’ =>‘application/json’}

rescue

puts(“Error encountered: #{$!.inspect}”)

end

end

puts "response: status = #{resp.status}"

puts "response: body = #{resp.body}"

fail "bad response #{resp.status} #{resp.reason}" unless resp.status == HTTP::Status::OK

page_content = resp.body

return page_content

end

For more functions and complete library you may refer to http://www.rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient

Advertisements
Categories: Uncategorized Tags: , ,