How to send a file from the http client to the server?

A

Abir B.

Hello
I've a http server built with ruby based on Mongrel, it must receive a
file from the client http (in the request coming from a web browser or
in request sent by a httpClient written in ruby)
How can I send the file from the http client to the server?
Thanks
 
R

Robert Dober

Hello
I've a http server built with ruby based on Mongrel, it must receive a
file from the client http (in the request coming from a web browser or
in request sent by a httpClient written in ruby)
How can I send the file from the http client to the server?
Thanks

Hmm the following might not work out of the box, but I copied the essential
stuff from my file server:

HTH
Robert
########################################################################
class UploadServlet < WEBrick::HTTPServlet::AbstractServlet
def upload_form res
styles =""
res.body = <<-EOS
<html>
<head>
<title>WEBrick File Upload Service</title>
#{styles}
</head>
<div class="headline">
WEBrick File Upload Service
</div>
<div class="input_form">
<form method="POST" enctype="multipart/form-data">
<input type="file" name="data" size="100" />
<div class="center leave_sp_ab1">
<input type="submit" value="Upload File" />
</div>
</form>
</div>
</html>
EOS

def do_GET(req, res)
res["content-type"] = "text/html"
upload_form res
end

def do_POST(req, res)
res['content-type']="text/html"

length = req['content-length'].to_i
#<snip>

upload_data = req.query["data"]
filename = upload_data.filename
File.open("whatever", "wb") do |file|
upload_data.each_data do |data|
file << data.to_s
end
end
end
end # class UploadServlet

svr =
WEBrick::HTTPServer.new(
:port => 4242,
:ListenAddress => "1.2.3.4")

svr.mount("/upload", UploadServlet,)

trap:)INT){ svr.shutdown }
svr.start
--------------------- 8< --------------------
 
A

Abir B.

Thanks for response
But what I need to do is to build the request. I must know how to code
the request or the Client.
 
R

Robert Dober

Thanks for response
But what I need to do is to build the request. I must know how to code
the request or the Client.

Well actually it is difficult to give the correct response if one does
not read the question :(
Sorry for the noise. Unfortunately I do not know how to encode the
multipart file, but hopefully somebody does, Pit?

Cheers
Robert
 
A

Abir B.

Hi
I created a form wich allows to select a file and send it to the server
:
-----------------
class FormHandler < Mongrel::HttpHandler
def process(req, resp)
resp.start do |head, body|
body << "<html>"
body << "<body>"
body << "<form name=""formulaire_envoi_fichier""
enctype=""multipart/form-data"" method=""post"" action=""./test"">"
body << "<input type=""file"" name=""fichier_choisi"">"
body << "<br>"
body << " <br>"
body << " <input type=""submit"" name=""bouton_submit""
value=""Envoyer le fichier"">"
body << "</body>"
body << "</html>"
end
end
end

class MyHandler < Mongrel::HttpHandler
attr_accessor :responses_file
def initialize
end
def process(req, resp)
resp.start do |head, body|
puts "Req.inspect",req.inspect
puts "Req params",req.params

i=0

req.body.each_line do |l|
puts l
end

puts
print i," lignes"

body.write("<html>");
body.write(" <head>");
body.write(" <title>Test Page</title>");
body.write(" </head>");
body.write(" <body>");
...
body << "</body></html>"
end
end
end
h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/test", MyHandler.new)
h.register("/acc",FormHandler.new)
h.run.join

end


---------------

Then when I run in the test page http://localhost:3000/test, I can see
the content of the file in the request's body (puts l). But I don't know
how to extract this data. I've to work on the string?
Another question, how can I send the file directly (without passing by
the form).
Thanks
 
R

Robert Dober

Hi
I created a form wich allows to select a file and send it to the server
:
-----------------
class FormHandler < Mongrel::HttpHandler
def process(req, resp)
resp.start do |head, body|
body << "<html>"
body << "<body>"
body << "<form name=""formulaire_envoi_fichier""
enctype=""multipart/form-data"" method=""post"" action=""./test"">"
body << "<input type=""file"" name=""fichier_choisi"">"
body << "<br>"
body << " <br>"
body << " <input type=""submit"" name=""bouton_submit""
value=""Envoyer le fichier"">"
body << "</body>"
body << "</html>"
end
end
end

class MyHandler < Mongrel::HttpHandler
attr_accessor :responses_file
def initialize
end
def process(req, resp)
resp.start do |head, body|
puts "Req.inspect",req.inspect
puts "Req params",req.params

i=0

req.body.each_line do |l|
puts l
end

puts
print i," lignes"

body.write("<html>");
body.write(" <head>");
body.write(" <title>Test Page</title>");
body.write(" </head>");
body.write(" <body>");
...
body << "</body></html>"
end
end
end
h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/test", MyHandler.new)
h.register("/acc",FormHandler.new)
h.run.join

end


---------------

Then when I run in the test page http://localhost:3000/test, I can see
the content of the file in the request's body (puts l). But I don't know
how to extract this data.

Now I am confused, look into the method do_POST of my first post and
you will see how to extract this data.
R.
 
R

Richard Conroy

Abir,
are you using a HTTP client library? Or are you using something
more basic like
raw net/http or open-uri ? You could cheat and use something a bit
more high level
like Hpricot.

Or are you using something non-Ruby like curl, and need to know exactly how you
pad out the HTTP Header?
 
A

Abir B.

Richard said:
Abir,
are you using a HTTP client library? Or are you using something
more basic like
raw net/http or open-uri ? You could cheat and use something a bit
more high level
like Hpricot.

Or are you using something non-Ruby like curl, and need to know exactly
how you
pad out the HTTP Header?

I used Ruby (Mongrel) to code the server, and for the client I must use
ruby because the file will be generated by the client and then sent to
the server, but I'm confused between httpclient, net/http, open-uri ...
I don't know the best choice wich respond to my needs.

P.S : Robert Dober I can't run your example because httprequest in
Mongrel is different from webrick (has only 2 attributes body and
params).

thanks
 
R

Robert Dober

I used Ruby (Mongrel) to code the server, and for the client I must use
ruby because the file will be generated by the client and then sent to
the server, but I'm confused between httpclient, net/http, open-uri ...
I don't know the best choice wich respond to my needs.

P.S : Robert Dober I can't run your example because httprequest in
Mongrel is different from webrick (has only 2 attributes body and
params).
strange I'd rather thought that the req object in your upload should
be the same?
 
R

Richard Conroy

I used Ruby (Mongrel) to code the server, and for the client I must use
ruby because the file will be generated by the client and then sent to
the server, but I'm confused between httpclient, net/http, open-uri ...
I don't know the best choice wich respond to my needs.

I feel your pain. I don't know httpclient directly, but net/http is very
low level and difficult to work with when you just want to get stuff
done, or validate a server design. Also open-uri, which is super-friendly
to use, can't use POST, which is a bit of a deal breaker.

AFAIK there isn't a solid contender in the Ruby Http Library space.

Also, you are sending up a file. As far as your Http Client is concerned,
it can ignore the 'file' input from the form.

You might want to take a look at some alternative HttpClients (e.g. Hpricot,
WATIR, WWW::Mechanize, or look at some open source application
specific http clients (like the 2 clients that are used to connect
Ruby with Amazon's S3 service).

Alternatively the O'Reilly RESTful Web Services book, has some advice
on this, and the authors made some modifications to open-uri to support
all the HTTP verbs: just gem install rest-open-uri, and you might get
open-uri friendliness without the deal breakers.
 
P

Phil Meier

Abir said:
I used Ruby (Mongrel) to code the server, and for the client I must use
ruby because the file will be generated by the client and then sent to
the server, but I'm confused between httpclient, net/http, open-uri ...
I don't know the best choice wich respond to my needs.

P.S : Robert Dober I can't run your example because httprequest in
Mongrel is different from webrick (has only 2 attributes body and
params).

thanks

By googling for "class Net::HTTP::post multipart" you can find an
article on www.pivotalblabs.com that has a solution on how to enhance
net/https (by defining a 'text_to_multipart' method etc.). I used this
code snippet and it works really well.

BR Phil
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top