Posting XML

Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
is with my code. It does not seem to do anything when I run it. Your
help is much appreciated!
Here is my code:

I am trying to post an XML file but it does not seem to do anything.
Can someone take a look at this code and tell me if it looks correct?
Thank you!

require 'rubygems'
require 'net/https'
require 'net/http'
require 'uri'
require 'nokogiri'

url = "http://mydomain.com/Will.do?method=ntf"
uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == 'https')

f = File.open('my.xml','r')
data = Nokogiri::XML(f)
f.close

headers = {'Content-Type' => 'text/xml'}

resp, body = http.post(uri.path, data.to_xml, headers)

Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
is with my code. It does not seem to do anything when I run it. Your
help is much appreciated!
Here is my code:

I am trying to post an XML file but it does not seem to do anything.

What exactly do you mean by that? Is it not sent to the server? Does
the server not react on it?

Can someone take a look at this code and tell me if it looks correct?
Thank you!

require 'rubygems'
require 'net/https'
require 'net/http'
require 'uri'
require 'nokogiri'

url = "http://mydomain.com/Will.do?method=ntf"
uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == 'https')

f = File.open('my.xml','r')
data = Nokogiri::XML(f)
f.close

headers = {'Content-Type' => 'text/xml'}

resp, body = http.post(uri.path, data.to_xml, headers)

Why do you read in the file, convert it to a DOM and then convert it
back before sending? IMHO that would only make sense if you either
need to validate the XML or change the structure.

Kind regards

robert

···

On Wed, Mar 9, 2011 at 9:10 AM, JesseQA <uscengineer@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi Robert. Thanks for the response. The file is not sent to the
server. You ask about converting the file to a DOM then back before
sending
Would I be able to remove the read/convert and instead use something
like this?

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == 'https')

headers = {'Content-Type' => 'text/xml'}
resp, body = http.post(uri.path, data.to_xml, headers)

···

On Mar 9, 12:31 am, Robert Klemme <shortcut...@googlemail.com> wrote:

On Wed, Mar 9, 2011 at 9:10 AM, JesseQA <uscengin...@gmail.com> wrote:
> Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
> is with my code. It does not seem to do anything when I run it. Your
> help is much appreciated!
> Here is my code:

> I am trying to post an XML file but it does not seem to do anything.

What exactly do you mean by that? Is it not sent to the server? Does
the server not react on it?

> Can someone take a look at this code and tell me if it looks correct?
> Thank you!

> require 'rubygems'
> require 'net/https'
> require 'net/http'
> require 'uri'
> require 'nokogiri'

> url = "http://mydomain.com/Will.do?method=ntf&quot;
> uri = URI.parse(url)

> http = Net::HTTP.new(uri.host, uri.port)
> http.use_ssl = true if (uri.scheme == 'https')

> f = File.open('my.xml','r')
> data = Nokogiri::XML(f)
> f.close

> headers = {'Content-Type' => 'text/xml'}

> resp, body = http.post(uri.path, data.to_xml, headers)

Why do you read in the file, convert it to a DOM and then convert it
back before sending? IMHO that would only make sense if you either
need to validate the XML or change the structure.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

Thanks for the response Robert.
Would I be able to post like this?

resp, body = http.post(uri.path, 'my.xml', headers)

···

On Mar 9, 12:31 am, Robert Klemme <shortcut...@googlemail.com> wrote:

On Wed, Mar 9, 2011 at 9:10 AM, JesseQA <uscengin...@gmail.com> wrote:
> Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
> is with my code. It does not seem to do anything when I run it. Your
> help is much appreciated!
> Here is my code:

> I am trying to post an XML file but it does not seem to do anything.

What exactly do you mean by that? Is it not sent to the server? Does
the server not react on it?

> Can someone take a look at this code and tell me if it looks correct?
> Thank you!

> require 'rubygems'
> require 'net/https'
> require 'net/http'
> require 'uri'
> require 'nokogiri'

> url = "http://mydomain.com/Will.do?method=ntf&quot;
> uri = URI.parse(url)

> http = Net::HTTP.new(uri.host, uri.port)
> http.use_ssl = true if (uri.scheme == 'https')

> f = File.open('my.xml','r')
> data = Nokogiri::XML(f)
> f.close

> headers = {'Content-Type' => 'text/xml'}

> resp, body = http.post(uri.path, data.to_xml, headers)

Why do you read in the file, convert it to a DOM and then convert it
back before sending? IMHO that would only make sense if you either
need to validate the XML or change the structure.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

Probably rather

resp, body = http.post(uri.path, File.read('my.xml'), headers)

or

File.open('my.xml', 'r') do |io|
  resp, body = http.post(uri.path, io, headers)
end

Note sure though whether the latter approach would work. "ri" is your friend.

Cheers

robert

···

On Wednesday, March 9, 2011 10:00:06 AM UTC+1, JesseQA wrote:

On Mar 9, 12:31 am, Robert Klemme <short...@googlemail.com> wrote:
> On Wed, Mar 9, 2011 at 9:10 AM, JesseQA <uscen...@gmail.com> wrote:
> > Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
> > is with my code. It does not seem to do anything when I run it. Your
> > help is much appreciated!
> > Here is my code:
>
> > I am trying to post an XML file but it does not seem to do anything.
>
> What exactly do you mean by that? Is it not sent to the server? Does
> the server not react on it?
>
>
>
> > Can someone take a look at this code and tell me if it looks correct?
> > Thank you!
>
> > require 'rubygems'
> > require 'net/https'
> > require 'net/http'
> > require 'uri'
> > require 'nokogiri'
>
> > url = "http://mydomain.com/Will.do?method=ntf&quot;
> > uri = URI.parse(url)
>
> > http = Net::HTTP.new(uri.host, uri.port)
> > http.use_ssl = true if (uri.scheme == 'https')
>
> > f = File.open('my.xml','r')
> > data = Nokogiri::XML(f)
> > f.close
>
> > headers = {'Content-Type' => 'text/xml'}
>
> > resp, body = http.post(uri.path, data.to_xml, headers)
>
> Why do you read in the file, convert it to a DOM and then convert it
> back before sending? IMHO that would only make sense if you either
> need to validate the XML or change the structure.

Thanks for the response Robert.
Would I be able to post like this?

resp, body = http.post(uri.path, 'my.xml', headers)

Thanks Robert I will give that a try.

···

On Mar 9, 1:21 am, Robert Klemme <shortcut...@googlemail.com> wrote:

On Wednesday, March 9, 2011 10:00:06 AM UTC+1, JesseQA wrote:
> On Mar 9, 12:31 am, Robert Klemme <short...@googlemail.com> wrote:
> > On Wed, Mar 9, 2011 at 9:10 AM, JesseQA <uscen...@gmail.com> wrote:
> > > Hi everyone. A newbie with ruby/xml so I am not sure where my trouble
> > > is with my code. It does not seem to do anything when I run it. Your
> > > help is much appreciated!
> > > Here is my code:

> > > I am trying to post an XML file but it does not seem to do anything.

> > What exactly do you mean by that? Is it not sent to the server? Does
> > the server not react on it?

> > > Can someone take a look at this code and tell me if it looks correct?
> > > Thank you!

> > > require 'rubygems'
> > > require 'net/https'
> > > require 'net/http'
> > > require 'uri'
> > > require 'nokogiri'

> > > url = "http://mydomain.com/Will.do?method=ntf&quot;
> > > uri = URI.parse(url)

> > > http = Net::HTTP.new(uri.host, uri.port)
> > > http.use_ssl = true if (uri.scheme == 'https')

> > > f = File.open('my.xml','r')
> > > data = Nokogiri::XML(f)
> > > f.close

> > > headers = {'Content-Type' => 'text/xml'}

> > > resp, body = http.post(uri.path, data.to_xml, headers)

> > Why do you read in the file, convert it to a DOM and then convert it
> > back before sending? IMHO that would only make sense if you either
> > need to validate the XML or change the structure.
> Thanks for the response Robert.
> Would I be able to post like this?

> resp, body = http.post(uri.path, 'my.xml', headers)

Probably rather

resp, body = http.post(uri.path, File.read('my.xml'), headers)

or

File.open('my.xml', 'r') do |io|
resp, body = http.post(uri.path, io, headers)
end

Note sure though whether the latter approach would work. "ri" is your friend.

Cheers

robert