Faraday: Middleware cripple requests

Created on 10 Jan 2015  ·  3Comments  ·  Source: lostisland/faraday

Without any middleware

Everything working as expected

[37] pry(main)>  p = Faraday.new("http://192.168.5.4:8112")
=> #<Faraday::Connection:0x007fd461259c88
 @builder=#<Faraday::RackBuilder:0x007fd4612598f0 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd461258c48 URL:http://192.168.5.4:8112/>>
[38] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
=> #<Faraday::Response:0x007fd4611d3bd8
 @env=
  #<struct Faraday::Env
   method=:post,
   body="{\"id\": 0, \"result\": false, \"error\": null}",
   url=#<URI::HTTP:0x007fd4611e21d8 URL:http://192.168.5.4:8112/json>,
   request=
    #<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
   request_headers={"User-Agent"=>"Faraday v0.9.1", "Content-Type"=>"application/x-www-form-urlencoded"},
   ssl=
    #<struct Faraday::SSLOptions
     verify=nil,
     ca_file=nil,
     ca_path=nil,
     verify_mode=nil,
     cert_store=nil,
     client_cert=nil,
     client_key=nil,
     certificate=nil,
     private_key=nil,
     verify_depth=nil,
     version=nil>,
   parallel_manager=nil,
   params=nil,
   response=nil,
   response_headers=
    {"transfer-encoding"=>"chunked",
     "date"=>"Sat, 10 Jan 2015 01:47:43 GMT",
     "connection"=>"close",
     "content-type"=>"application/x-json",
     "server"=>"TwistedWeb/12.0.0"},
   status=200>,
 @on_complete_callbacks=[]>

With Logger middleware:

Probably does not make any reponse. Crash on dumping headers (they are nil)

[39] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.response :logger }
=> #<Faraday::Connection:0x007fd460a44f98
 @builder=#<Faraday::RackBuilder:0x007fd460a44930 @handlers=[Faraday::Response::Logger]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd460a44520 URL:http://192.168.5.4:8112/>>
[40] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
I, [2015-01-10T04:48:16.268374 #77388]  INFO -- : post http://192.168.5.4:8112/json
D, [2015-01-10T04:48:16.268467 #77388] DEBUG -- request: User-Agent: "Faraday v0.9.1"
I, [2015-01-10T04:48:16.268619 #77388]  INFO -- Status: 
NoMethodError: undefined method `map' for nil:NilClass
from /Users/tehkode/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/faraday-0.9.1/lib/faraday/response/logger.rb:36:in `dump_headers'

With JSON request middleware

Does not make any request. Return request as response

[41] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.request :json }
=> #<Faraday::Connection:0x007fd460c68e78
 @builder=#<Faraday::RackBuilder:0x007fd460c68a40 @handlers=[FaradayMiddleware::EncodeJson]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd460c68770 URL:http://192.168.5.4:8112/>>
[42] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
=> #<Faraday::Response:0x007fd460b922b0
 @env=
  #<struct Faraday::Env
   method=:post,
   body="{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"torrent\"]}",
   url=#<URI::HTTP:0x007fd460b92c38 URL:http://192.168.5.4:8112/json>,
   request=
    #<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
   request_headers={"User-Agent"=>"Faraday v0.9.1", "Content-Type"=>"application/json"},
   ssl=
    #<struct Faraday::SSLOptions
     verify=nil,
     ca_file=nil,
     ca_path=nil,
     verify_mode=nil,
     cert_store=nil,
     client_cert=nil,
     client_key=nil,
     certificate=nil,
     private_key=nil,
     verify_depth=nil,
     version=nil>,
   parallel_manager=nil,
   params=nil,
   response=nil,
   response_headers=nil,
   status=nil>,
 @on_complete_callbacks=[]>

With JSON response middleware

[43] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.response :json }
=> #<Faraday::Connection:0x007fd4611eaae0
 @builder=#<Faraday::RackBuilder:0x007fd4611ea680 @handlers=[FaradayMiddleware::ParseJson]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd4611ea360 URL:http://192.168.5.4:8112/>>
[44] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"torrent\"]}"
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/tehkode/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/faraday_middleware-0.9.1/lib/faraday_middleware/response_middleware.rb:59:in `response_type'

All 3 comments

Sorry for the confustion. When you pass a ruby block to configure middleware, you have to explicitly also configure the adapter as well otherwise nothing will work. https://github.com/lostisland/faraday/issues/325#issuecomment-30370061

@mislav Probably good idea would be to fallback to default adapter if its not specified explicitly. Current behavior is very confusing.

@mislav another option is to raise a descriptive exception if there is no adapter set in the block

I agree that the current behavior is pretty confusing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yusefu picture yusefu  ·  3Comments

jeffb-stell picture jeffb-stell  ·  5Comments

luizkowalski picture luizkowalski  ·  3Comments

amrrbakry picture amrrbakry  ·  4Comments

QuinnWilton picture QuinnWilton  ·  4Comments