Ruby Tuesday – Net::HTTP::get

Today’s Ruby Tuesday is a short one, it is on Net::HTTP::get.

There are times when you need a very basic ability to make a HTTP call, and Net::HTTP::get can help you with that.

To first be able to use Net::HTTP::get you need to require net/http.

require 'net/http'

Net::HTTP::get takes either a URI object, or a hostname, path and optional port, which defaults to port 80 if not specified. Net::HTTP::get returns a string that is the body of the response.

Net::HTTP.get(URI('http://www.example.com'))
# => "<!doctype html>n<html>n<head>n    <title>Example Domain</title>nn    <meta charset="utf-8" />n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />n    <meta name="viewport" content="width=device-width, initial-scale=1" />n    <style type="text/css">n    body {n        background-color: #f0f0f2;n        margin: 0;n        padding: 0;n        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;n        n    }n    div {n        width: 600px;n        margin: 5em auto;n        padding: 50px;n        background-color: #fff;n        border-radius: 1em;n    }n    a:link, a:visited {n        color: #38488f;n        text-decoration: none;n    }n    @media (max-width: 700px) {n        body {n            background-color: #fff;n        }n        div {n            width: auto;n            margin: 0 auto;n            border-radius: 0;n            padding: 1em;n        }n    }n    </style>    n</head>nn<body>n<div>n    <h1>Example Domain</h1>n    <p>This domain is established to be used for illustrative examples in documents. You may use thisn    domain in examples without prior coordination or asking for permission.</p>n    <p><a href="http://www.iana.org/domains/example">More information...</a></p>n</div>n</body>n</html>n"
Net::HTTP.get('www.example.com', '/')
# => "<!doctype html>n<html>n<head>n    <title>Example Domain</title>nn    <meta charset="utf-8" />n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />n    <meta name="viewport" content="width=device-width, initial-scale=1" />n    <style type="text/css">n    body {n        background-color: #f0f0f2;n        margin: 0;n        padding: 0;n        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;n        n    }n    div {n        width: 600px;n        margin: 5em auto;n        padding: 50px;n        background-color: #fff;n        border-radius: 1em;n    }n    a:link, a:visited {n        color: #38488f;n        text-decoration: none;n    }n    @media (max-width: 700px) {n        body {n            background-color: #fff;n        }n        div {n            width: auto;n            margin: 0 auto;n            border-radius: 0;n            padding: 1em;n        }n    }n    </style>    n</head>nn<body>n<div>n    <h1>Example Domain</h1>n    <p>This domain is established to be used for illustrative examples in documents. You may use thisn    domain in examples without prior coordination or asking for permission.</p>n    <p><a href="http://www.iana.org/domains/example">More information...</a></p>n</div>n</body>n</html>n"
[11] pry(main)> res = Net::HTTP.get('www.example.com', '/')
=> "<!doctype html>n<html>n<head>n    <title>Example Domain</title>nn    <meta charset="utf-8" />n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />n    <meta name="viewport" content="width=device-width, initial-scale=1" />n    <style type="text/css">n    body {n        background-color: #f0f0f2;n        margin: 0;n        padding: 0;n        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;n        n    }n    div {n        width: 600px;n        margin: 5em auto;n        padding: 50px;n        background-color: #fff;n        border-radius: 1em;n    }n    a:link, a:visited {n        color: #38488f;n        text-decoration: none;n    }n    @media (max-width: 700px) {n        body {n            background-color: #fff;n        }n        div {n            width: auto;n            margin: 0 auto;n            border-radius: 0;n            padding: 1em;n        }n    }n    </style>    n</head>nn<body>n<div>n    <h1>Example Domain</h1>n    <p>This domain is established to be used for illustrative examples in documents. You may use thisn    domain in examples without prior coordination or asking for permission.</p>n    <p><a href="http://www.iana.org/domains/example">More information...</a></p>n</div>n</body>n</html>n"
res.class
# => String

Be warned, that this is for very simple cases, and the documentation notes that this is not recommended if you are doing performing many HTTP requests, as it does not persist the connection.

If you need more features or a higher level of abstraction for making HTTP requests I would suggest checking out Faraday or Typhoeus, but I do want to call out that there is Net::HTTP::get if you need it.

–Proctor