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

S

scootiePuff

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
<dataset>.. through </dataset>, 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: <dataset>...</dataset>' 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' ?>
<DATASET>
<SITE_ID>123456789</SITE_ID>
<MLID>987654321</MLID>
<DATA type='email'>[email protected]</DATA>
</DATASET>
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:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not
understand.<br />
</p>
</body></html>

.. 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.
 
J

Jeff

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
 
S

scootiePuff

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'>[email protected]</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.)
 
S

scootiePuff

(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
 
R

Rilindo Foster

scootiePuff said:
(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 )
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top