Code Monkey home page Code Monkey logo

Comments (7)

sferik avatar sferik commented on August 15, 2024

Note: The source code for the relevant middleware lives in the faraday_middleware gem: https://github.com/pengwynn/faraday_middleware/tree/master/lib/faraday/response

from faraday.

mislav avatar mislav commented on August 15, 2024

This is not breakage, it's something I actually fixed in Faraday. From the README:

The order in which middleware is stacked is important. Like with Rack, the first middleware on the list wraps all others, while the last middleware is the innermost one, so that's usually the adapter.

Before my refactoring, Faraday respected this to some extent, but the order in which on_complete handlers fired was a total mess and very inconsistent. Now there's only one rule: request middleware fires in order in which they were added, response middleware fires in the reverse order. Adapters come last in the bottom. This is the standard Rack way.

Your stack should look like:

Faraday.new(options) do |builder|
  builder.request :multipart
  builder.request :url_encoded
  builder.use Faraday::Request::OAuth, authentication if authenticated?
  builder.use Faraday::Response::Mashify unless raw
  unless raw
    case format.to_s.downcase
    when 'json'
      builder.use Faraday::Response::ParseJson
    when 'xml'
      builder.use Faraday::Response::ParseXml
    end
  end
  builder.use Faraday::Response::RaiseHttp4xx
  builder.use Faraday::Response::RaiseHttp5xx
  builder.adapter(adapter)
end

Now XML/JSON will first get parsed, then mashified.

One more note: the bundled Multipart middleware is a little different than the one you removed. The bundled one still doesn't handle "File" instances.

from faraday.

sferik avatar sferik commented on August 15, 2024

Thanks for explaining. The ordering was confusing me.

Is there a reason why the built-in Multipart middleware doesn't handle "File" instances? Would it make sense to replace the one in faraday with the one in the twitter gem, which handles files correctly?

Or maybe I should add MultipartWithFile middleware to the faraday_middleware gem?

from faraday.

sferik avatar sferik commented on August 15, 2024

I just ran into another issue where env[:http_headers] used to return {} when it was blank, but now it returns nil. Was this intentional?

from faraday.

mislav avatar mislav commented on August 15, 2024

I'd welcome that you contribute File handling together with mime-type guessing based on filename.

As for http_headers: you must have confused that with request_headers or response_headers.

from faraday.

sferik avatar sferik commented on August 15, 2024

Yes, I meant response_headers. Was that an intention move to change the default to nil when they're empty? I needed to make this change to make it work: jnunemaker/twitter@97644a9#diff-1

from faraday.

mislav avatar mislav commented on August 15, 2024

That's a bug. Is this with the test adapter? Please file an issue.

Luckily, in real world response headers are never empty.

from faraday.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.