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
On 10/23/07, Abir B. [email protected] wrote:
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?
ThanksPosted via http://www.ruby-forum.com/.
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
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
#
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< --------------------
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.
On 10/23/07, Abir B. [email protected] wrote:
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.Posted via http://www.ruby-forum.com/.
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
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 << “”
body << “”
body << “<form name=”“formulaire_envoi_fichier”"
enctype=““multipart/form-data”” method=““post”” action=“”./test"“>”
body << “<input type=”“file”" name=““fichier_choisi””>"
body << “
”
body << "
"
body << " <input type=““submit”” name=““bouton_submit””
value=““Envoyer le fichier””>"
body << “”
body << “”
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
On 10/23/07, Abir B. [email protected] wrote:
enctype=““multipart/form-data”” method=““post”” action=“”./test"“>”
body.write(" </head>");
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.
Richard C. wrote:
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 D. I can’t run your example because httprequest in
Mongrel is different from webrick (has only 2 attributes body and
params).
thanks
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?
On 10/23/07, Abir B. [email protected] wrote:
Richard C. wrote:
Abir,
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.
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.
Abir B. schrieb:
pad out the HTTP Header?
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
On 10/23/07, Abir B. [email protected] wrote:
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 D. 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?
By googling for “class Net::HTTP::Post multipart” you can find an
article on www.pivotalblabs.com
The direct URL for this article is
http://pivots.pivotallabs.com/users/damon/blog/articles/227-standup-04-27-07-testing-file-uploads