<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Multipart POST in Ruby</title>
	<atom:link href="http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/</link>
	<description>Keith on XML, Publishing, Ruby, Birds, &#038; San Francisco</description>
	<lastBuildDate>Wed, 09 Sep 2009 14:05:30 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Keith</title>
		<link>http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/comment-page-1/#comment-124</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Sun, 04 Jan 2009 02:54:32 +0000</pubDate>
		<guid isPermaLink="false">http://kfahlgren.com/blog/?p=24#comment-124</guid>
		<description>Thanks, Stanislav.</description>
		<content:encoded><![CDATA[<p>Thanks, Stanislav.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stanislav.vitvitskiy</title>
		<link>http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/comment-page-1/#comment-123</link>
		<dc:creator>stanislav.vitvitskiy</dc:creator>
		<pubDate>Thu, 25 Dec 2008 15:23:07 +0000</pubDate>
		<guid isPermaLink="false">http://kfahlgren.com/blog/?p=24#comment-123</guid>
		<description>The above post is partial. So I posted the full source on my own blog http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html</description>
		<content:encoded><![CDATA[<p>The above post is partial. So I posted the full source on my own blog <a href="http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html" rel="nofollow">http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stanislav.vitvitskiy</title>
		<link>http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/comment-page-1/#comment-122</link>
		<dc:creator>stanislav.vitvitskiy</dc:creator>
		<pubDate>Thu, 25 Dec 2008 14:52:04 +0000</pubDate>
		<guid isPermaLink="false">http://kfahlgren.com/blog/?p=24#comment-122</guid>
		<description>I played around it a bit and came up with the class more suitable for bigger files ( such as video ).

&lt;pre&gt;
class Multipart
  
  def initialize( file_names )
    @file_names = file_names
  end
  
  def post( to_url )
    boundary = &#039;----RubyMultipartClient&#039; + rand(1000000).to_s + &#039;ZZZZZ&#039;
    
    parts = []
    streams = []
    @file_names.each do &#124;param_name, filepath&#124;
      pos = filepath.rindex(&#039;/&#039;)
      filename = filepath[pos + 1, filepath.length - pos]
      parts &lt;&lt; StringPart.new ( &quot;--&quot; + boundary + &quot;\r\n&quot; +
      &quot;Content-Disposition: form-data; name=\&quot;&quot; + param_name.to_s + &quot;\&quot;; filename=\&quot;&quot; + filename + &quot;\&quot;\r\n&quot; +
      &quot;Content-Type: video/x-msvideo\r\n\r\n&quot;)
      stream = File.open(filepath, &quot;rb&quot;)
      streams &lt;&lt; stream
      parts &lt;&lt; StreamPart.new (stream, File.size(filepath))
    end
    parts &lt;= @parts.size
      return nil;
    end
    
    how_much_current_part = @parts[@part_no].size - @part_offset
    
    how_much_current_part = if how_much_current_part &gt; how_much
      how_much
    else
      how_much_current_part
    end
    
    how_much_next_part = how_much - how_much_current_part
    
    current_part = @parts[@part_no].read(@part_offset, how_much_current_part )

    if how_much_next_part &gt; 0
      @part_no += 1
      @part_offset = 0
      next_part = read ( how_much_next_part  )
      current_part + if next_part
        next_part
      else
        &#039;&#039;
      end
    else
      @part_offset += how_much_current_part
      current_part
    end
  end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I played around it a bit and came up with the class more suitable for bigger files ( such as video ).</p>
<pre>
class Multipart

  def initialize( file_names )
    @file_names = file_names
  end

  def post( to_url )
    boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'

    parts = []
    streams = []
    @file_names.each do |param_name, filepath|
      pos = filepath.rindex('/')
      filename = filepath[pos + 1, filepath.length - pos]
      parts &lt;&lt; StringPart.new ( "--" + boundary + "\r\n" +
      "Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
      "Content-Type: video/x-msvideo\r\n\r\n")
      stream = File.open(filepath, "rb")
      streams &lt;&lt; stream
      parts &lt;&lt; StreamPart.new (stream, File.size(filepath))
    end
    parts &lt;= @parts.size
      return nil;
    end

    how_much_current_part = @parts[@part_no].size - @part_offset

    how_much_current_part = if how_much_current_part &gt; how_much
      how_much
    else
      how_much_current_part
    end

    how_much_next_part = how_much - how_much_current_part

    current_part = @parts[@part_no].read(@part_offset, how_much_current_part )

    if how_much_next_part &gt; 0
      @part_no += 1
      @part_offset = 0
      next_part = read ( how_much_next_part  )
      current_part + if next_part
        next_part
      else
        ''
      end
    else
      @part_offset += how_much_current_part
      current_part
    end
  end
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith</title>
		<link>http://kfahlgren.com/blog/2006/11/01/multipart-post-in-ruby-2/comment-page-1/#comment-102</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Wed, 13 Jun 2007 16:05:38 +0000</pubDate>
		<guid isPermaLink="false">http://kfahlgren.com/blog/?p=24#comment-102</guid>
		<description>Someone asked me how this actually gets used. Here&#039;s a quick example:

&lt;pre&gt;

    
  def self.post_form(url, query, headers)
    Net::HTTP.start(url.host, url.port) {&#124;con&#124;
      con.read_timeout = TIMEOUT_SECONDS
      begin
        return con.post(url.path, query, headers)
      rescue =&gt; e
        puts &quot;POSTING Failed #{e}... #{Time.now}&quot;
      end  
    }   
  end 
  
    # my server expects the params of the POST to
    # use the rails-like &quot;blah[bar]&quot; syntax, and I
    # need to send two things other than the file
    # itself. These are stored in the params Hash
    params = Hash.new
    
    # Open the actually file I want to send
    file = File.open(filename, &quot;rb&quot;)

    # set the params to meaningful values
    params[&quot;file[new_file]&quot;] = file
    params[&quot;file[abc_filename]&quot;] = abc_filename
    params[&quot;xyz&quot;] = xyz
    
    # make a MultipartPost
    mp = Multipart::MultipartPost.new

    # Get both the headers and the query ready,
    # given the new MultipartPost and the params 
    # Hash
    query, headers = mp.prepare_query(params)
    
    # done with file now
    file.close
    
    # Make sure the URL is useable
    url = URI.parse(URL)
    
    # Do the actual POST, given the right inputs
    res = post_form(url, query, headers)
    
    # res holds the response to the POST
    case res 
    when Net::HTTPSuccess
      puts &quot;Hooray&quot;
    when Net::HTTPInternalServerError
      raise &quot;Server blew up&quot;
    else
      raise &quot;Unknown error #{res}: #{res.inspect}&quot;
    end  
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Someone asked me how this actually gets used. Here&#8217;s a quick example:</p>
<pre>

  def self.post_form(url, query, headers)
    Net::HTTP.start(url.host, url.port) {|con|
      con.read_timeout = TIMEOUT_SECONDS
      begin
        return con.post(url.path, query, headers)
      rescue => e
        puts "POSTING Failed #{e}... #{Time.now}"
      end
    }
  end 

    # my server expects the params of the POST to
    # use the rails-like "blah[bar]" syntax, and I
    # need to send two things other than the file
    # itself. These are stored in the params Hash
    params = Hash.new

    # Open the actually file I want to send
    file = File.open(filename, "rb")

    # set the params to meaningful values
    params["file[new_file]"] = file
    params["file[abc_filename]"] = abc_filename
    params["xyz"] = xyz

    # make a MultipartPost
    mp = Multipart::MultipartPost.new

    # Get both the headers and the query ready,
    # given the new MultipartPost and the params
    # Hash
    query, headers = mp.prepare_query(params)

    # done with file now
    file.close

    # Make sure the URL is useable
    url = URI.parse(URL)

    # Do the actual POST, given the right inputs
    res = post_form(url, query, headers)

    # res holds the response to the POST
    case res
    when Net::HTTPSuccess
      puts "Hooray"
    when Net::HTTPInternalServerError
      raise "Server blew up"
    else
      raise "Unknown error #{res}: #{res.inspect}"
    end
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
