Home > Uncategorized > Ruby: Using URI and HTTPClient

Ruby: Using URI and HTTPClient

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: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: