never_had_a_name
never_had_a_name

Reputation: 93216

REST with ruby?

Are there good references teaching you how to send PUT/DELETE/POST/GET with ruby?

I have looked at Net::HTTP.

Is this library capable of all four methods? I couldn't find how to send with PUT.

Are there other good libraries for all these four methods?

Upvotes: 3

Views: 600

Answers (3)

lsdr
lsdr

Reputation: 1235

You can do all HTTP verbs with net/http library. Other libraries are an option as well - HTTParty is nice, and I personally like faraday.

With net/http, you could explore verbs doing something like this:

require 'net/http'

http = Net::HTTP.new('api.host.ca')

# GET, DELETE
http.get('/path')
http.delete('/path')

# POST, PUT
http.put('/path', body_data)
http.post('/path', body_data)

Where body_data is whatever you want to send over the wire. Is also worth noting that all four methods can receive a Hash as an optional third parameter with the HTTP Request-Headers;

# GET, with Headers
http.get('/path', { 'Content-Type' => 'application/json' })

This is, obviously, the very basic.

Consider playing with Google APIs and Ruby to get a hang of it.

Upvotes: 0

yxhuvud
yxhuvud

Reputation: 108

The simplest way would probably be to use the rest client gem. Then you can do stuff like

RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}

EDIT: changed url to a more up to date one.

Upvotes: 3

Daniel O'Hara
Daniel O'Hara

Reputation: 13438

You should definitely look at HTTParty. It's an easy to use library to deal with RESTful requests, JSON responses and so forth.

Upvotes: 4

Related Questions