Net/HTTP: A Brief Introduction

Thomas Dubiel
1 min readMay 2, 2021

--

There are many HTTP clients that can be utilized in your Ruby applications such as Faraday, rest-client, httparty and Typhoeus. Many of these, however, are third-party implementations of Net/HTTP, which is part of Ruby’s Standard Library. This is the client that we will be examining today.

In order to start executing http requests, we will create a new ruby file and at the top, require the “net/http” gem.

require “net/http”

GET

The #get method allows us to simply retrieve data from a page:

page = URI('https://en.wikipedia.org/wiki/Main_Page')
response = Net::HTTP.get(page)
puts response

In the above code, we are passing the website url to URI, which will parse the data, and we are storing that string in the variable page. Next, we are calling the get method from the HTTP library and passing in the page. When we puts the return value of that method call, we get a giant string containing the entire HTML markup of the main Wikipedia page!

--

--

Thomas Dubiel
Thomas Dubiel

No responses yet