The Ruby Toolbox lists no less than 25 HTTP clients. Let’s look at how to retrieve and parse JSON results from a RESTful API using the four most popular Ruby HTTP libraries.

The four code snippets below will:

Define a URL to be parsed. We’ll use the Spotify API because it allows requests without authentication. Make an HTTP GET request to that URL. Parse the JSON result.

Each snippet is a different path to the same destination; if you pp the result from any of them, you’ll see a hash with Spotify search results:

Once you have tried a few, head over to our documentation site and try some Ruby Quickstarts. But for now, onward!

net/http

net/http is built into the Ruby standard library. There’s no gem to install and no dependencies to cause headaches down the road. If you value stability over speed of development, this is the right choice for you (for example, the twilio-ruby gem is built on net/http).

Unsurprisingly, using Net/http can require more work than using the gems built on top of it. For example, you have to create a URI object before making the HTTP request.