How to use the httparty Ruby gem
In the last section, How to work with APIs in Ruby, we built a class to call an external API. In this section, we are going to walk through some shortcuts for using the httparty
Ruby gem and working with the Stack Overflow API.
Let's start by creating a variable called response
and pass a URL to the get
method of HTTParty
:
require 'rubygems' require 'httparty' response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
Now we'll see the different features provided by the built-in HTTParty
method.
First, there is the body
method that can be called with this code:
response.body
This will give the following output:

This is the body of all the items available in the Stack Exchange API that we accessed through the get
method of HTTParty
. You can see different attributes on every page. You can even copy one of the URLs from the body and check it in the browser.
Now, instead of body
, let's see what happens when you use a method called code
:
puts...