简体   繁体   English

休息与红宝石?

[英]REST with ruby?

Are there good references teaching you how to send PUT/DELETE/POST/GET with ruby? 有没有很好的参考资料教你如何用红宝石发送PUT / DELETE / POST / GET?

I have looked at Net::HTTP. 我看过Net :: HTTP。

Is this library capable of all four methods? 这个库能够用于所有四种方法吗? I couldn't find how to send with PUT. 我找不到如何用PUT发送。

Are there other good libraries for all these four methods? 所有这四种方法都有其他好的库吗?

You should definitely look at HTTParty . 你一定要看看HTTParty It's an easy to use library to deal with RESTful requests, JSON responses and so forth. 它是一个易于使用的库来处理RESTful请求,JSON响应等等。

The simplest way would probably be to use the rest client gem . 最简单的方法可能是使用rest客户端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. 您可以使用net/http库执行所有HTTP谓词。 Other libraries are an option as well - HTTParty is nice, and I personally like faraday . 其他库也是一个选项 - HTTParty很好,我个人喜欢faraday

With net/http , you could explore verbs doing something like this: 使用net/http ,你可以探索动词做这样的事情:

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. body_data是你想要通过电线发送的任何东西。 Is also worth noting that all four methods can receive a Hash as an optional third parameter with the HTTP Request-Headers; 还值得注意的是,所有四种方法都可以通过HTTP Request-Headers接收Hash作为可选的第三个参数;

# 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. 考虑使用Google API和Ruby来掌握它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM