Ruby 中的 REST 客户端示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8452410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST Client Example in Ruby
提问by rubythemystery
Can anyone explain me with an example, by using REST Client to do GET/POST/PUT operations in a Rest web service?
任何人都可以通过使用 REST 客户端在 Rest Web 服务中执行 GET/POST/PUT 操作来举例说明吗?
In POST/PUT, using REST Client, need to pass the whole xml body to do POST/PUT operations.
在POST/PUT中,使用REST Client,需要传递整个xml body来做POST/PUT操作。
For example, Using REST Client
例如,使用 REST Client
I need to get the content of a service using,
我需要使用,获取服务的内容,
RESTClient.get(url)
POST an xml to an url:
将 xml 发布到 url:
RESTClient.post(url,entirexml)
PUT an xml to an URL:
PUT 一个 xml 到一个 URL:
RESTClient.put(url,entirexml)
DELETE using REST CLIENT.
使用 REST 客户端删除。
Can anyone help me with examples for all the REST Client HTTP METHODS with example?
任何人都可以通过示例帮助我提供所有 REST 客户端 HTTP 方法的示例吗?
I need to send the whole XML along with namespace to a rest service using PUT/POST operations of REST Client.
我需要使用 REST 客户端的 PUT/POST 操作将整个 XML 以及命名空间发送到休息服务。
If anyone have examples on this, kindly post then please.
如果有人有这方面的例子,请张贴然后。
回答by jmontross
require 'rest-client'
RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
RestClient.get 'http://example.com/resource'
xml = '<xml><foo>bar</foo><bar>foo</bar></xml>'
RestClient.post 'http://example.com/resource', xml , {:content_type => :xml}
RestClient.put 'http://example.com/resource', xml , {:content_type => :xml}
RestClient.delete 'http://example.com/resource'
See more examples and documentation at https://github.com/rest-client/rest-client
回答by Chuck van der Linden
The Readme file at the git site for the rest-client gemhas a whole bunch of examples of how to do requests, include parameters, etc.
git 站点上 rest-client gem的自述文件有一大堆关于如何处理请求、包含参数等的示例。
I'd start with that.
我会从那个开始。
If there are specific things that are not working, then it generally helps to post the code you've tried that you think SHOULD be working, and then it's usually easier for people to tell where you are going wrong.
如果有特定的东西不起作用,那么发布您尝试过的您认为应该工作的代码通常会有所帮助,然后人们通常更容易告诉您哪里出错了。

