post xml over https connection

B

brad

Has anyone posted xml data over a https connection? We have a vendor
product that has an API that allows for programmatic operation. Data is
exchanged in xml format. The exchange occurs over https. Any tips or
ideas would be appreciated. I can open https connection, but I'm not
sure how to post raw XML over the connections.

Thanks
 
R

Robert Klemme

Has anyone posted xml data over a https connection? We have a vendor
product that has an API that allows for programmatic operation. Data is
exchanged in xml format. The exchange occurs over https. Any tips or
ideas would be appreciated. I can open https connection, but I'm not
sure how to post raw XML over the connections.

Just like with HTTP: set the content type to "text/xml" and send (post)
the data.

Kind regards

robert
 
B

brad

Robert said:
Just like with HTTP: set the content type to "text/xml" and send (post)
the data.

Kind regards

robert

Perhaps I'm doing something wrong. Here's what I'm trying:

nex = Net::HTTP.new("https://127.0.0.1", port)
nex.use_ssl = true
path = '/path/to/xml_interface/'
headers = {'Content-Type' => 'text/xml'}
resp, data = nex.post(path, data, headers)

I get this error:

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize': getaddrinfo: Name or
service not known (SocketError)
from /usr/lib/ruby/1.8/net/http.rb:560:in `open'
from /usr/lib/ruby/1.8/net/http.rb:560:in `connect'
from /usr/lib/ruby/1.8/timeout.rb:48:in `timeout'
from /usr/lib/ruby/1.8/timeout.rb:76:in `timeout'
from /usr/lib/ruby/1.8/net/http.rb:560:in `connect'
from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
from /usr/lib/ruby/1.8/net/http.rb:542:in `start'
from /usr/lib/ruby/1.8/net/http.rb:1032:in `request'
from /usr/lib/ruby/1.8/net/http.rb:842:in `post'

The code works up until the actual post attempt. Any ideas?
 
K

Keith Fahlgren

Robert Klemme wrote:
Perhaps I'm doing something wrong. Here's what I'm trying:

nex = Net::HTTP.new("https://127.0.0.1", port)
nex.use_ssl = true
path = '/path/to/xml_interface/'
headers = {'Content-Type' => 'text/xml'}
resp, data = nex.post(path, data, headers)

(Not sure that 'data' is set correctly above...)

You'll want to pass HTTP.new an address, not a URL:
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

Something like:
require 'net/https'
require 'uri'
url = "https://127.0.0.1:7891/path/to/xml_interface"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == 'https')
data = "foo"
headers = {'Content-Type' => 'text/xml'}
# warning, uri.path will drop queries, use uri.path + uri.query if you need to
resp, body = nex.post(uri.path, data, headers)


HTH,
Keith
 
B

brad

Keith said:
You'll want to pass HTTP.new an address, not a URL:
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html

Something like:
require 'net/https'
require 'uri'
url = "https://127.0.0.1:7891/path/to/xml_interface"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == 'https')
data = "foo"
headers = {'Content-Type' => 'text/xml'}
# warning, uri.path will drop queries, use uri.path + uri.query if you need to
resp, body = nex.post(uri.path, data, headers)


HTH,
Keith

That was silly of me. Thank you for the example and the doc pointer. I
have it working now.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top