[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie problems with Net::Http

Marcelo (PC)

5/18/2005 11:25:00 PM

Hi there!

I'm starting to use Net::Http.

I'm fetching an image from a page with the following code


Net::HTTP.start(address) do |http|
#req = Net::HTTP::Get.new('http://a page')
req = Net::HTTP::Get.new('http://a page/a_image.JPG')
req.basic_auth 'user', 'password'
response = http.request(req)
file = File.new('file.jpg',"w")
file.write(response.body)
file.close

the problem is that the file created has additional info and the image
can't be readed.. Any idea what I'm doing wrong?




--
Este correo esta libre de virus!



2 Answers

Assaph Mehr

5/19/2005 12:18:00 AM

0


Open the file in binary mode to write the image:

> Net::HTTP.start(address) do |http|
> #req = Net::HTTP::Get.new('http://a page')
> req = Net::HTTP::Get.new('http://a page/a_image.JPG')
> req.basic_auth 'user', 'password'
> response = http.request(req)
> file = File.new('file.jpg',"w")
> file.write(response.body)
> file.close

Instead of the above 3 lines:
File.open('file.jpg, 'wb') { |f| f.write response.body }

Which will open the file for binary writing and close it for you after
the block is executed.

BTW, the argument for Get.new can be just the path of the image on the
site, without the http://site part.

HTH,
Assaph

Marcelo (PC)

5/19/2005 4:09:00 PM

0

Thank! it was really helpful! now it is running ok.

Marcelo

Assaph Mehr escribió:

>Open the file in binary mode to write the image:
>
>
>
>> Net::HTTP.start(address) do |http|
>> #req = Net::HTTP::Get.new('http://a page')
>> req = Net::HTTP::Get.new('http://a page/a_image.JPG')
>> req.basic_auth 'user', 'password'
>> response = http.request(req)
>> file = File.new('file.jpg',"w")
>> file.write(response.body)
>> file.close
>>
>>
>
>Instead of the above 3 lines:
> File.open('file.jpg, 'wb') { |f| f.write response.body }
>
>Which will open the file for binary writing and close it for you after
>the block is executed.
>
>BTW, the argument for Get.new can be just the path of the image on the
>site, without the http://site part.
>
>HTH,
>Assaph
>
>
>
>
>

--
Este correo esta libre de virus!