HTTParty is a Ruby gem that simplifies HTTP requests and JSON parsing. HTTParty automatically detects JSON responses based on the Content-Type
header and parses them into Ruby objects for easy manipulation.
Automatic JSON Parsing
HTTParty automatically parses JSON responses when the server returns a Content-Type: application/json
header:
require 'httparty'
# Make a GET request to a JSON API
response = HTTParty.get('https://jsonplaceholder.typicode.com/users/1')
# HTTParty automatically parses the JSON response
user_data = response.parsed_response
puts user_data['name'] # Access data like a regular Ruby hash
puts user_data['email'] # user_data is a Hash object
puts user_data.class # => Hash
Accessing Response Data
HTTParty provides multiple ways to access the parsed JSON data:
response = HTTParty.get('https://jsonplaceholder.typicode.com/posts')
# Method 1: Using parsed_response (recommended)
posts = response.parsed_response
puts posts.first['title']
# Method 2: Direct access (same as parsed_response for JSON)
posts = response.body
puts posts.first['title'] if posts.is_a?(Array)
# Method 3: Check response status and handle accordingly
if response.success?
posts = response.parsed_response
posts.each { |post| puts post['title'] }
else
puts "Request failed: #{response.code} #{response.message}"
end
Working with Different JSON Structures
Array Responses
# API returns an array of objects
response = HTTParty.get('https://jsonplaceholder.typicode.com/posts')
posts = response.parsed_response
posts.each do |post|
puts "Title: #{post['title']}"
puts "Body: #{post['body']}"
puts "---"
end
Object Responses
# API returns a single object
response = HTTParty.get('https://jsonplaceholder.typicode.com/users/1')
user = response.parsed_response
puts "Name: #{user['name']}"
puts "Email: #{user['email']}"
puts "Address: #{user['address']['city']}, #{user['address']['street']}"
Manual JSON Parsing
If you need to force JSON parsing regardless of the Content-Type header:
require 'httparty'
require 'json'
class JsonParser < HTTParty::Parser
def parse
JSON.parse(body)
end
end
response = HTTParty.get('https://api.example.com/data', parser: JsonParser)
data = response.parsed_response
Error Handling and Best Practices
Always implement proper error handling when working with JSON APIs:
require 'httparty'
def fetch_user_data(user_id)
response = HTTParty.get("https://jsonplaceholder.typicode.com/users/#{user_id}")
# Check HTTP status
unless response.success?
puts "HTTP Error: #{response.code} #{response.message}"
return nil
end
# Parse and return data
response.parsed_response
rescue HTTParty::Error => e
puts "HTTParty Error: #{e.message}"
nil
rescue JSON::ParserError => e
puts "JSON Parsing Error: #{e.message}"
nil
rescue StandardError => e
puts "Unexpected Error: #{e.message}"
nil
end
# Usage
user = fetch_user_data(1)
if user
puts "User: #{user['name']} (#{user['email']})"
else
puts "Failed to fetch user data"
end
POST Requests with JSON
HTTParty also handles JSON responses from POST requests:
require 'httparty'
# Send JSON data and receive JSON response
response = HTTParty.post('https://jsonplaceholder.typicode.com/posts',
body: {
title: 'My New Post',
body: 'This is the post content',
userId: 1
}.to_json,
headers: {
'Content-Type' => 'application/json'
}
)
if response.success?
created_post = response.parsed_response
puts "Created post with ID: #{created_post['id']}"
else
puts "Failed to create post: #{response.code}"
end
Advanced Response Inspection
For debugging and development, inspect the response object:
response = HTTParty.get('https://jsonplaceholder.typicode.com/users/1')
puts "Status Code: #{response.code}"
puts "Headers: #{response.headers}"
puts "Content-Type: #{response.headers['content-type']}"
puts "Response Class: #{response.parsed_response.class}"
puts "Raw Body: #{response.body}"
HTTParty makes JSON parsing seamless by automatically detecting and parsing JSON responses, while providing flexibility for custom parsing scenarios and comprehensive error handling.