ROR汇集---Httparty
来源:互联网 发布:海口有mac口红专柜吗 编辑:程序博客网 时间:2024/05/16 07:04
httparty
DESCRIPTION:
Makes http fun again!
FEATURES/PROBLEMS:
- Easy get, post, put, delete requests
- Basic http authentication
- Default request query string parameters (ie: for api keys that are needed on each request)
- Automatic parsing of JSON and XML into ruby hashes based on response content-type
SYNOPSIS:
The following is a simple example of wrapping Twitter‘s API for posting updates.
That is really it! The object returned is a ruby hash that is decoded from Twitter‘s json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples).
That works and all but what if you don‘t want to embed your username and password in the class? Below is an example to fix that:
REQUEST OPTIONS
Each of the HTTP method (get, post, put and delete) each take a hash of options. The following keys can be specified in the options:
headers:A Hash of header key/value pairs query:A Hash of query key/value pairs body:The body of the request. If it‘s a Hash, it is converted into query-string format, otherwise it is sent as-is. basic_auth:A Hash containing keys for :username and :password. no_follow:Turns off automatic redirect followingREQUIREMENTS:
- Active Support >= 2.1
INSTALL:
- sudo gem install httparty
以下是我个人的一些使用:
服务一(接受参数,返回结果):
首先在一个服务中定义一个Restful接口(在Controller中配置):
在这里可能有些人疑问,为什么要加上@item = Item.new unless @item ,具体原因我也不清楚,只是当@item为空时,会出现类似下面的错误:
ActionView::MissingTemplate (Missing template items/get_item_by_name.erb in view path app/views)
详细看图:
虽然这个错误并不会影响到最终结果,但是错误就是错误,必须解决。
要被使用,还必须定义路由:
接着确认一些该接口是否可用:http://website_one_url[:port]/items/get_item_by_name.json?avatar_name=头像1
如:http://localhost:3000/items/get_item_by_name.json?avatar_name=头像1
服务器二(POST参数,接受结果)
1、安装httparty插件
2、使用起来也很简单,只要在需要调用上面接口的Controller中简单设置一些就可以了:
如果有@item不为空,那么返回来的值的格式应该是(json):
{"item"=>{"name"=>"电子优惠券", "price"=>50,"avatar_updated_at"=>nil}}
就是一个哈希,获取值必须像这样item["item"]["name"],不能item[:item][:name],单引号也不行。
参考:
http://httparty.rubyforge.org/rdoc/
http://httparty.rubyforge.org/
http://rdoc.info/projects/jnunemaker/httparty
http://github.com/jnunemaker/httparty/tree/master
- ROR汇集---Httparty
- ROR汇集---Transaction
- ROR汇集---Routing
- ROR汇集---Rending
- ROR汇集---Layout
- ROR汇集---Render
- ROR汇集---will_paginate
- ROR汇集---restful-authentication
- ROR汇集---memcached
- ROR汇集---easy-fckeditor
- ROR汇集---aasm
- ROR汇集---Permissions Control
- ROR汇集---Authlogic
- ROR
- ROR
- ROR
- ROR
- ror
- C#的Form通过CLI调用C++的DLL
- CAsyncSocket,CSocket的使用方法
- 追求人格完善和对山水审美意境的发掘
- ROR汇集---will_paginate
- 没有芥菜子交给佛陀
- ROR汇集---Httparty
- 面试题 10套
- 一个简单的生成包和发包的程序
- ROR汇集---restful-authentication
- 十个利用矩阵乘法解决的经典题目
- ROR汇集---memcached
- ROR汇集---easy-fckeditor
- lujin
- 双面与VFACE