net/http and binary files

Discussion in 'Ruby' started by Constantin Gavrilescu, Sep 9, 2007.

  1. I'm using the code from the example
    http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html for the
    http/net library:

    File.open('result.txt', 'w') {|f|
    http.get('/~foo/') do |str|
    f.write str
    end
    }


    This works fine for html files. But for binary files the result is a
    corrupted file.
    I've run this in the console:
    >> Net::HTTP.start('localhost', 80){|http|File.open('result.mp3', 'w'){|f|http.g

    et('/crocodil2/adrian.mp3') {|str|f.write str}}}
    => #<Net::HTTPOK 200 OK readbody=true>

    The mp3 files is corrupted. It does not seem to be base64 encoded...
    help please.
    --
    Posted via http://www.ruby-forum.com/.
     
    Constantin Gavrilescu, Sep 9, 2007
    #1
    1. Advertising

  2. Constantin Gavrilescu wrote:
    > I'm using the code from the example
    > http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html
    > This works fine for html files. But for binary files the result is a
    > corrupted file.
    > I've run this in the console:
    >>> Net::HTTP.start('localhost', 80){|http|File.open('result.mp3', 'w'){|f|http.g

    > et('/crocodil2/adrian.mp3') {|str|f.write str}}}
    > => #<Net::HTTPOK 200 OK readbody=true>
    > The mp3 files is corrupted. It does not seem to be base64 encoded...
    > help please.


    The example works on *nix systems, but on Windoze, you must write the
    file in binary mode: File.open('result.mp3', 'wb')

    Clifford Heath.
     
    Clifford Heath, Sep 9, 2007
    #2
    1. Advertising

  3. Clifford Heath wrote:

    > The example works on *nix systems, but on Windoze, you must write the
    > file in binary mode: File.open('result.mp3', 'wb')



    Wonderful. Forgot about that... Thank you.
    --
    Posted via http://www.ruby-forum.com/.
     
    Constantin Gavrilescu, Sep 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dermoon
    Replies:
    0
    Views:
    514
    dermoon
    Oct 8, 2003
  2. utab
    Replies:
    3
    Views:
    906
  3. rodrigo
    Replies:
    0
    Views:
    363
    rodrigo
    Jul 9, 2003
  4. Eric Luo
    Replies:
    3
    Views:
    315
    Martin DeMello
    Jun 4, 2005
  5. Jim
    Replies:
    6
    Views:
    765
Loading...

Share This Page