8

I have the following code

conn = Faraday.new(:url => 'http://www.mapquestapi.com') do |faraday|
 faraday.response :logger                  # log requests to STDOUT
 faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
 faraday.options.params_encoder = Faraday::FlatParamsEncoder
end

response = conn.get do |req|
   req.url '/geocoding/v1/batch'
   req.params['key'] = 'xxxxx%7xxxxxxxxx%2xxxx%3xxxx-xxxx'
   req.params['location'] = addresses[0]
end

Unfortunately the key param gets encoded as shown in the log in this way key=xxxxx%257xxxxxxxxx%252xxxx%253xxxx-xxxx, causing that the mapquest API respond to with an invalid key (due to the encoding as i tried with postman and it works)

I, [2014-01-22T19:16:17.949640 #93669]  INFO -- : get http://www.mapquestapi.com/geocoding/v1/batch?key=xxxxx%257xxxxxxxxx%252xxxx%253xxxx-xxxx&location=1047+East+40th+Avenue%2C+Vancouver%2C+BC+V5W+1M5%2C+Canada
D, [2014-01-22T19:16:17.949778 #93669] DEBUG -- request: User-Agent: "Faraday v0.9.0"
accept-encoding: ""
I, [2014-01-22T19:16:19.038914 #93669]  INFO -- Status: 200
D, [2014-01-22T19:16:19.039043 #93669] DEBUG -- response: server: "Apache-Coyote/1.1"
set-cookie: "JSESSIONID=AD0A86636DAAD3324316A454354F; Path=/; HttpOnly"

The key param should be send without encoding the %, how can i avoid that this doesnt happen, i haven't found any parameter to change this behavior

Im using Faraday 0.9.0, ruby 2.0. I know i can use the mapquest library that relies on restclient gem but as i ve spent some time coding this it would be nice how to make it work with faraday

4

2 回答 2

6

这只是部分答案:

可以创建自己的 param_encorder 以避免值字段被转义。这在这里使用(参见方法 build_exclusive_url) ruby​​-2.0.0-p247@zingtech/gems/faraday-0.9.0/lib/faraday/connection.rb

class DoNotEncoder
  def self.encode(params)
    buffer = ''
    params.each do |key, value|
      buffer << "#{key}=#{value}&"
    end
    return buffer.chop
  end
end

conn = Faraday.new(:url => 'http://www.mapquestapi.com') do |faraday|
 faraday.response :logger                  # log requests to STDOUT
 faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
 #faraday.options.params_encoder = Faraday::FlatParamsEncoder
 faraday.options.params_encoder = DoNotEncoder
end

但 !!查询字符串未通过查询文本检查(请参阅 check_query 方法) ruby​​-2.0.0-p247/lib/ruby/2.0.0/uri/generic.rb

我怀疑它由于其他原因而失败。该站点是使用 CSRF 还是您需要捕获 cookie 并将其包含在您的请求中。

如果您需要使用 CSRF,请查看https://gist.github.com/chrisZingel/9042812以获取代码示例。

于 2014-03-09T21:27:24.800 回答
1

在你需要法拉第之后添加这个。

module Faraday
  module NestedParamsEncoder
    def self.escape(arg)
      arg
    end
  end
  module FlatParamsEncoder
    def self.escape(arg)
      arg
    end
  end
end
于 2015-02-24T02:03:28.470 回答