Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Errors while file uploading thr 'net/http' or 'rest-open-uri' client
saurabh purnaye
10/24/2008 8:43:00 AM
[Note: parts of this message were removed to make it a legal post.]
I am trying to upload a file to API thr the 'net/http' or 'rest-open-uri'
client,
the code I have done is
require 'rest-open-uri'
require 'uri'
require 'cgi'
require 'net/http'
require 'net/https'
require 'mime/types'
username="XXXXXXXXXXXXXXXXXXXXXXX"
password="XXXXXXXXXXXXXXXXXXXXXX"
def form_encoded(hash)
encoded = []
hash.each do |key, value|
encoded << CGI.escape(key) + '=' + CGI.escape(value)
end
return encoded.join('&')
end
boundary_token = [Array.new(8) {rand(256)}].join
boundary_marker = "--#{boundary_token}\r\n"
file = File.open("my_img.jpg")
filename = File.basename(file.path)
mime_type = MIME::Types.of(filename)
@service_root = "#######################"
representation =
form_encoded({ "Content-Type"=>"#{mime_type}\r\n\r\n#{file.read}\r\n","Content-Transfer-Encoding"
=> "binary\r\n", "Content-Disposition"
=> "form-data" , "uploaded_data[filename]"=>"#{filename}"})
puts representation
response = open(@service_root + '/file/uploads.xml', :method
=> :post,:http_basic_authentication => [username,
password],:body=>representation )
puts response.read
The error I am getting is...
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:659:in
`buffer_open'
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:194:in
`open_loop'
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:192:in
`catch'
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:192:in
`open_loop'
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:162:in
`open_uri'
from
/lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:561:in
`open'
from /lib/ruby/gems/1.8/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:35:in
`open'
I am not really sure about the way to be done!
I have referred the urls
http://stackoverflow.com/questions/116353/uploading-files-in-rub...
http://www.realityforge.org/articles/2006/03/02/upload-a-file-via-post-wit...
2 Answers
Brian Candler
10/24/2008 2:45:00 PM
0
>
http://www.realityforge.org/articles/2006/03/02/upload-a-file-via-post-wit...
That's the document I was going to point you at. It Works For Me [TM],
for an application which expects to get a multipart/form-data POST.
Now, what you are doing seems very strange; you appear to be
form-encoding the Content-Type and Content-Transfer-Encoding headers and
sending them in the body instead of sending them as HTTP headers.
Furthermore, the place you are posting to is
http://something/file/uploads.xml which looks like you should be sending
an XML document instead of form-encoded data anyway. And you are
creating a MIME boundary string too, even though you're not sending it!
You should be aware that there are at least three completely different
ways of uploading data via a POST:
(1) Raw content in the body, such as an image or an XML document
(2) application/x-www-form-encoded, where the body consists of
name1=value1&name2=value2&...
(3) multipart/form-data, where the body consists of multiple MIME
sections, one for each parameter in the form [this is the usual case for
uploads of "attachments" through a web browser]
If you try to use the wrong way of uploading the data, or worse try to
mix all three together as you appear to be doing, it will be meaningless
to the server.
Since there are so many variables and possible things to go wrong, you
first need to work out what on earth it is you're trying to do in the
first place. You need to work from something concrete, such as:
(a) the actual server-side code which receives the data; or
(b) a HTML form which successfully uploads the required data via a
browser; or
(c) a tcpdump of a successful upload from some other client; or
(d) a hard-and-fast accurate technical specification for the format the
server is expecting to receive.
Then at least it should be possible to work out which of formats (1),
(2) or (3) you need to send, and then you can google for some working
code which does that.
Brian.
--
Posted via
http://www.ruby-...
.
saurabh purnaye
10/24/2008 3:23:00 PM
0
[Note: parts of this message were removed to make it a legal post.]
I have used this code, which has solved my problem,
A gem curb is used, which made my code extremely easy!
require 'rubygems'
require 'curb'
c = Curl::Easy.new("
https://the_url_goes....
)
c.multipart_form_post = true
c.http_post(Curl::PostField.content('uploaded_wav','saurabh_upload.wav'),Curl::PostField.content('api_key',
'00000000000000000000000000Curl::PostField.content('action',
'broadcast.create'),Curl::PostField.content('name',
'test_saurabh'),Curl::PostField.content('number_list', '123456789'))
puts c.body_str
puts c.header_str
puts c.methods.sort
On Fri, Oct 24, 2008 at 8:15 PM, Brian Candler <b.candler@pobox.com> wrote:
> >
>
http://www.realityforge.org/articles/2006/03/02/upload-a-file-via-post-wit...
>
> That's the document I was going to point you at. It Works For Me [TM],
> for an application which expects to get a multipart/form-data POST.
>
> Now, what you are doing seems very strange; you appear to be
> form-encoding the Content-Type and Content-Transfer-Encoding headers and
> sending them in the body instead of sending them as HTTP headers.
>
> Furthermore, the place you are posting to is
> http://something/file/uploads.xml which looks like you should be sending
> an XML document instead of form-encoded data anyway. And you are
> creating a MIME boundary string too, even though you're not sending it!
>
> You should be aware that there are at least three completely different
> ways of uploading data via a POST:
>
> (1) Raw content in the body, such as an image or an XML document
>
> (2) application/x-www-form-encoded, where the body consists of
> name1=value1&name2=value2&...
>
> (3) multipart/form-data, where the body consists of multiple MIME
> sections, one for each parameter in the form [this is the usual case for
> uploads of "attachments" through a web browser]
>
> If you try to use the wrong way of uploading the data, or worse try to
> mix all three together as you appear to be doing, it will be meaningless
> to the server.
>
> Since there are so many variables and possible things to go wrong, you
> first need to work out what on earth it is you're trying to do in the
> first place. You need to work from something concrete, such as:
>
> (a) the actual server-side code which receives the data; or
> (b) a HTML form which successfully uploads the required data via a
> browser; or
> (c) a tcpdump of a successful upload from some other client; or
> (d) a hard-and-fast accurate technical specification for the format the
> server is expecting to receive.
>
> Then at least it should be possible to work out which of formats (1),
> (2) or (3) you need to send, and then you can google for some working
> code which does that.
>
> Brian.
> --
> Posted via
http://www.ruby-...
.
>
>
--
--
Thanks and Regards
Saurabh Purnaye
+91-9922071155
skype: sorab_pune
yahoo & gtalk: saurabh.purnaye
msn: psaurabh@live.com
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Errors while file uploading thr 'net/http' or 'rest-open-uri' client
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password