lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.7k stars 971 forks source link

Unable to use webdav verbs #1532

Closed sdalu closed 7 months ago

sdalu commented 7 months ago

Connection#run_request checks for verb in Connection::METHODS.

Unfortunetaly Connection::METHODS is defined ad Set.new %i[get post put delete head patch options trace] which doesn't allows PROPFIND, PROPPATCH, MKCOL, COPY, ...

iMacTia commented 7 months ago

Hi @sdalu, that shouldn't be the case, run_request allows you to specify any method you like, including the ones from the WebDAV's extension. I tried this locally to confirm (tested with Faraday 2.7.11):

res = Faraday.new.run_request(:copy, 'http://httpbingo.org', nil, {})

res.headers # check headers to ensure the request happened
# => {"access-control-allow-credentials"=>"true",
# "access-control-allow-origin"=>"*",
# "content-type"=>"text/plain; charset=utf-8",
# "x-content-type-options"=>"nosniff",
# "date"=>"Tue, 14 Nov 2023 09:38:44 GMT",
# "content-encoding"=>"gzip",
# "transfer-encoding"=>"chunked",
# "server"=>"Fly/442f90d3 (2023-11-07)",
# "via"=>"1.1 fly.io",
# "fly-request-id"=>"01HF6K53ZP9SERP27MBTT8G93K-lhr",
# "cf-team"=>"1c6ecba7e0000048c3fb9f3400000001"}

res.body # we even got a response from httpbingo
# => "method COPY not allowed\n"

Could you please share more details around what you're trying and what error you're getting from Faraday, exactly?

sdalu commented 7 months ago

On 2.7.11 as well:

require 'faraday'

f = Faraday.new(url: 'http://httpbingo.org') do |f|
end

f.run_request(:proppatch, "/foo", nil, {})
/usr/local/lib/ruby/gems/3.2/gems/faraday-2.7.11/lib/faraday/connection.rb:433:in `run_request': unknown http method: proppatch (ArgumentError)

        raise ArgumentError, "unknown http method: #{method}"
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    from t.rb:6:in `<main>'
iMacTia commented 7 months ago

You're right, I was messing with Faraday::Connection::METHODS before testing the call and I ended up adding :copy to it, which made my code work 🤦. Sorry about that!

Faraday::Connection::METHODS is not frozen, so you should be able to add all the WebDAV methods to it:

Faraday::Connection::METHODS.merge([:copy, :lock, :mkcol, :move, :propfind, :proppatch, :unlock])

After that, you'll be able to use run_request with all of them 👍. This looks like a reasonable hack for the time being, especially considering these are not as widely used as the standard HTTP verbs.

I appreciate you were probably looking to make them first-class citizens like the others, and potentially make them methods like Faraday.copy or connection.proppatch, but I feel like that addition feels unnecessary in the core Faraday library.

If you'd like to see that happening, then my suggestion would be to instead build an extension gem (faraday-webdav?) that adds these verbs to the list of allowed verbs and also defines the missing methods ❤️ .

I'm closing this issue because of the problem described being "solved" with the hack, but if you're interested in the extension idea and would like discussing it more, then I'd encourage you to open a discussion instead 🙌 !