Constructing an (xml) request properly for http post to an api

hello,

goal: send a request that includes xml syntax via http post to
emaillabs’ api
problem: would like help in constructing the (xml) request properly

if one scrolls to the bottom of the emaillabs api overview
documentation here (http://www.emaillabs.com/email-marketing/api-
faq.html) under ‘What is an example of an API command?’,

notice the ‘type’ and ‘activity’ parameters. these, along with the
‘input’ parameter which is the xml ‘input from customer’ from
… through , are required according to the api
reference guide.

i’m using the net/http toolkit to try to post the request to emaillabs
so that i might receive some xml response back.

question: how would you incorporate the ‘type: record’, ‘activity:
query-data’, and ‘input: …’ parameters all
together in the (xml) request?

i tried to send everything together, but i suspect that this is not
correct.
in a ruby file, let’s say i have the following:

#!/usr/bin/env ruby

require ‘net/http’
require ‘rexml/document’
include REXML

target_url = ‘http://www.uptilt.com
path = ‘/API/mailing_list.html’

http = Net::HTTP.new(target_url)

xml_request =<<EOF
Type:record
Activity:query-data
Input:

<? xml version='1.0' ?> 123456789 987654321 [email protected] EOF

xml_response = http.post(path, xml_request)

puts xml_response.body

(the rexml is for me to parse the xml data once i am able to receive a
response.)

i get the following when running the ruby file:

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

… so that i suspect my request isn’t constructed properly. i’ve also
tried using ‘END’ in lieu of ‘EOF’, and double quotes to hold the
data, but i’m guessing that the type/activity/input are the problem.
i’ve also tried to put them in xml tags, but that still produced the
same ‘response’. i’ve also ensured that the api security settings are
set up properly, and that i’m contacting the correct url location
(different from target_url, path, and in the emaillabs api faq
example).

how should my (xml) request be constructed? please advise.

On Nov 28, 7:15 pm, scootiePuff [email protected] wrote:

hello,

goal: send a request that includes xml syntax via http post to
emaillabs’ api
problem: would like help in constructing the (xml) request properly

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

You don’t need those @ variables of course, I’m just copying and
pasting from our code, but hopefully it sheds some light. The main
difference is we’re constructing the first part differently, and
making sure we escape the xml first.

Jeff
softiesonrails.com
essentialrails.com

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' ?> 123456789 987654321 [email protected] 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.)

(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:increate_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:ininitialize’
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:ininitialize’
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:inbuild’
from /usr/local/lib/ruby/site_ruby/1.8/rexml/document.rb:45:in
initialize' from (irb):17:innew’
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 [email protected] wrote:

987654321
puts res.body

send email via emailLabs

@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

scootiePuff wrote:

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

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?

Didn’t see this ever answered, so for posterity’s sake - Assuming that
resp is Net::HTTPOK object:

doc = Document.new(resp.read_body)
doc.write( $stdout, 0 )

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs