How to avoid a Faraday query to encode receive parameters?

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 parameter receives the encoding, as shown in the log in this way key = xxxxx% 257xxxxxxxxx% 252xxxx% 253xxxx-xxxx , as a result of which the mapquest API responds with an invalid key (due to the encoding, as I tried with the 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 parameter should be sent without encoding % , how can I avoid this, this does not happen, I did not find any parameters to change this behavior

Im using Faraday 0.9.0, ruby ​​2.0. I know that I can use the mapquest library, which relies on restclient stones, but since I spent some time coding it would be nice to get it working with faraday

+6
source share
2 answers

This is only a partial answer:

You can create your own param_encorder to avoid escaping the value field. This is used here (see build_exclusive_url method) 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 

But!! The query string does not check the query text (see check_query method) ruby-2.0.0-p247 / lib / ruby ​​/2.0.0/uri/generic.rb

I suspect this is due to something else. Is the site using CSRF or do you need to grab a cookie and include it in your request.

If you need CSRF check out https://gist.github.com/chrisZingel/9042812 for sample code.

+3
source

Add this after you need faraday.

 module Faraday module NestedParamsEncoder def self.escape(arg) arg end end module FlatParamsEncoder def self.escape(arg) arg end end end 
+2
source

All Articles