Faraday 是一个RubyHTTP 客户端开发库,提供了一个通用的接口,支持如下适配器:

示例代码:

conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
  faraday.request  :url_enCODEd             # form-encode POST params
  faraday.response :logger                  # log Requests to STDOUT
  faraday.adapter  Faraday.default_adapter  # Make requests with Net::HTTP
end

# set proxy with string
conn.options.proxy = "http://user:password@example.org/"

# set proxy with hash
conn.options.proxy = { :uri => 'http://user:passwoRDExample.org' }

# SPEcify proxy user/pass
conn.options.proxy = { :uri => 'http://user:pass.org',
  :user => 'user',
  :password => 'pass' }

## GET ##

response = conn.get '/Nigiri/sake.JSON'     # GET http://sushi.com/nigiri/sake.json
response.body

conn.get '/nigiri', { :name => 'Maguro' }   # GET /nigiri?name=Maguro

conn.get do |req|                           # GET http://sushi.com/search?page=2&limit=100
  req.url '/search', :page => 2
  req.params['limit'] = 100
end

## POST ##

conn.post '/nigiri', { :name => 'Maguro' }  # POST "name=maguro" to http://sushi.com/nigiri

# post payload as JSON instead of "www-form-urlencoded" encoding:
conn.post do |req|
  req.url '/nigiri'
  req.headers['Content-Type'] = 'application/json'
  req.body = '{ "name": "Unagi" }'
end

## Per-request options ##

conn.get do |req|
  req.url '/search'
  req.options.timeout = 5           # open/read timeout in seconds
  req.options.open_timeout = 2      # conNECtion open timeout in seconds

  # set proxy with string
  req.options.proxy = "http://user:password@example.org/"

  # set proxy with hash
  req.options.proxy = { :uri => 'http://user:passwordexample.org' }

  # specify proxy user/pass
  req.options.proxy = { :uri => 'http://user:pass.org',
    :user => 'user',
    :password => 'pass' }
end
Faraday 0.12.0.1 发布,Ruby 的 HTTP 客户端开发库
Faraday 0.12.0.1 发布了,该版本解决了 TravisCI 在 Rubygems 上的部署问题。Faraday 是一个 Ruby 的 HTTP 客户端开发库,提供了一个通用的接口,支持如下适配器:Net::HTTPExconTyphoeusPatronEventMachine下载地址:Source code (zip)Source code (tar.gz)

发布于 2017-04-02 23:54:07 | 127 次阅读


Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务