Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart post with a zip file #42

Closed
ldonnet opened this issue Apr 17, 2015 · 2 comments
Closed

Multipart post with a zip file #42

ldonnet opened this issue Apr 17, 2015 · 2 comments

Comments

@ldonnet
Copy link

ldonnet commented Apr 17, 2015

I want to make a post request with faraday with a zip file and it fails with multipart-post.

I have this code :

   conn = Faraday.new(:url => "mon_url") do |conn|
          # POST/PUT params encoders:
        conn.request :multipart
        conn.request :url_encoded

        conn.adapter :net_http
    end

    file = StringIO.new( { "parameters" => {
                                     "neptune-import" => {
                                       "no_save" => false,
                                       "user_name" => "user",
                                       "name" => "Mon test",
                                       "organisation_name" => "organisation",
                                       "referential_name" => "test",
                                     }
                                   }
                  }.to_s )
    action_params_io = Faraday::UploadIO.new(file, "application/json", "parameters.json")

    transport_data_io = Faraday::UploadIO.new(::Zip::File.open("demo.zip"), "application/zip", "demo.zip")

    payload = {
                                                  :file1 => action_params_io,
                                                  :file2 => transport_data_io,
                        }

conn.post "http://", payload 

And it returns an error on length method :

NoMethodError: undefined method `length' for #<Zip::File:0x007f0cb455eae8>
    from /home/luc/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/multipart-post-2.0.0/lib/composite_io.rb:102:in `method_missing'
    from (irb):47

Do you have any idea how I could do that with your gem?

Thanks for your time and your work
Luc Donnet

@sodabrew
Copy link

It looks like you are trying to decompress this file before you upload it. Are you sure you want to do that?

A zip file contains a directory structure, so it would be problematic to provide a length parameter that represents the size of all of its contents. It would also be problematic to try and present the directory structure output from a zip file as a single upload file.

You may want to read the documentation for Zip::File http://www.rubydoc.info/github/rubyzip/rubyzip/Zip/File

@ioquatix
Copy link
Member

ioquatix commented May 3, 2017

@sodabrew thanks - yeah, this is obviously a user issue, should really just be providing path to UploadIO.

@ioquatix ioquatix closed this as completed May 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants