开发者

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?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜