(was Re: constructing an (xml) request properly for http post to an
api)


THE PROBLEM:
the rexml tutorial here (http://www.germane-software.com/software/
rexml/docs/tutorial.html) suggests that Document.new expects a
string.  my xml_response is of type Net::HTTPOK, not String.  i see
that i can not easily convert this into a string with
String.new(xml_response).

is there currently a (simple) way to convert the Net::HTTPOK object
into a format (string) useable by REXML::Document, and if so, what
might that be/what would you suggest?

if i am approaching this problem the wrong way, what might you advise?


SOME BACKGROUND:
i am having trouble successfully running the rexml/document's .new
method.

i have the following in my ruby script:

require 'rexml/document'
include REXML
.
.
.

(i successfully receive an xml response with a Net::HTTP.start call as
viewable via irb here called 'xml_response'..)

doc = Document.new(xml_response)


i am presented with the following error messages:
irb(main):017:0> doc = REXML::Document.new(res)
NameError: undefined local variable or method `source' for
REXML::SourceFactory:Class
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/source.rb:20:in
`create_from'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/parsers/baseparser.rb:
130:in `stream='
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/parsers/baseparser.rb:
107:in `initialize'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/parsers/treeparser.rb:
8:in `new'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/parsers/treeparser.rb:
8:in `initialize'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/document.rb:190:in `new'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/document.rb:190:in
`build'
	from /usr/local/lib/ruby/site_ruby/1.8/rexml/document.rb:45:in
`initialize'
	from (irb):17:in `new'
	from (irb):17


i am imitating syntax from these posts:
http://tinyurl.com/3958n5
http://tinyurl.com/yo3mel

the rexml tutorial here (http://www.germane-software.com/software/
rexml/docs/tutorial.html) suggests that Document.new expects a
string.  my xml_response is of type Net::HTTPOK, not String.  i see
that i can not easily convert this into a string with
String.new(xml_response).

is there currently a (simple) way to convert the Net::HTTPOK object
into a format (string) useable by REXML::Document, and if so, what
might that be/what would you suggest?

if i am approaching this problem the wrong way, what might you advise?

how were the other posts (http://tinyurl.com/3958n5, http://tinyurl.com/yo3mel)
able to utilize REXML::Document immediately -- i suspect that their
Net::HTTP .get and .new calls produced strings?

thank you for your time and help.



On Dec 3, 7:06 pm, scootiePuff <elle.on.ra... / gmail.com> wrote:
> thanks for responding jeff, and for your feedback.
>
> unfortunately, i kept encountering "syntax error, unexpected $end,
> expecting kEND"
> with variations of the send_email code.
>
> i tried the following instead, and i'm now able to receive an xml
> response:
>
> #!/usr/bin/env ruby
>
> require 'net/http'
> require 'uri'
>
> type = 'record'
> activity = 'query-data'
>
> xml_req =<<EOF
> <? xml version='1.0' ?>
> <DATASET>
> <SITE_ID>123456789</SITE_ID>
> <MLID>987654321</MLID>
> <DATA type='email'>u... / somedomain.com</DATA>
> </DATASET>
> EOF
>
> req = "type=#{type}&activity=#{activity}&input="
> req = req + URI.escape(xml_req)
>
> res = Net::HTTP.start('www.uptilt.com') { |http|
>   http.post("/API/mailing_list.html", req)}
>
> puts res.body
>
> (by the way, it is not an e-mail that i am attempting to send, but
> rather an e-mail address, in order that i might receive statistical
> information via xml that is tied to that e-mail address.)
>
> On Nov 28, 8:20 pm, Jeff <cohen.j... / gmail.com> wrote:
>
> > I think you're creating the xml part ok, it's the previous section
> > that should be different.  Here's what we do:
>
> > # send email via emailLabs
> > # the msg parameter is actually the XML fragment that should be sent
> > def send_email(msg, type, activity)
> >   req = "type=#{type}&activity=#{activity}&input="
> >   req = req + URI.escape(msg)
> >   begin
> >     Net::HTTP.start('www.uptilt.com') do |query|
> >        response = query.post("/API/mailing_list.html", req)
> >       @response = response.body
> >    end
> >  rescue
> >     @error = true
> >     @error_msg = "Unable to connect to Email Labs."
> >  return false
> > end

the initial post, in case is of interest:
http://tinyurl.com/2g5pt8