2

The below script works however, I'm receiving a 400 "Bad Request" response because the POST must include the below XML data into the HTTP body. I've been trying different ways to get this done with no success. Could someone point me in the right direction?

XML Property file:

<?xml version="1.0" encoding="UTF-8"?>
<spa:data xmlns:spa="http://www.Sparrow.com/" object="Sparrow.PropertyList.1">
               <spa:proplist>
                              <spa:propval name="username">jer</spa:propval>
               </spa:proplist>
</spa:data>

Ruby code:

#!/usr/bin/env ruby
require 'net/http'
require 'net/https'
require 'rest-client'
require 'base64'


uri = URI.parse('http://sparrow:3453/rest/api/enum')
xml_data = %{<?xml version="1.0" encoding="UTF-8"?>
<spa:data xmlns:spa="http://www.Sparrow.com/" object="Sparrow.PropertyList.1">
<spa:proplist>
<spa:propval name="username">jer</spa:propval>
</spa:proplist>
</spa:data>}
res = req.post(uri.path, xml_data, {'Content-Type' => 'text/xml', 'Content-Length' =>  xml_data.length.to_s, "User-Agent" => "VAS-UCIP/3.1/1.0", "Connection" => "keep-alive" })
puts res.body
req = Net::HTTP::Post.new(uri)

puts "Authenticating..."
req.basic_auth 'user', 'secret'
puts "Authenticated..."
#req.ssl = false

puts "Sending XML data..."


puts "Starting HTTP request..."
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end

case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.value
end
#puts res.body
nanotechz9l
  • 141
  • 2
  • 10

1 Answers1

2

use the below:

   #!/usr/bin/env ruby
  require 'net/http'
  require 'base64'
  uri = URI.parse('http://www.google.com')
  req = Net::HTTP.new(uri.hostname, uri.port)
  xml_data = %{<?xml version="1.0" encoding="UTF-8"?>
  <spa:data xmlns:spa="http://www.Sparrow.com/" object="Sparrow.PropertyList.1">
                 <spa:proplist>
                                <spa:propval name="username">jer</spa:propval>
                 </spa:proplist>
  </spa:data>}
  user_and_pass = username + ':' + password #replace username and password with your username and password as strings
  base64user_and_pass = Base64.encode64(user_and_pass)
  res = req.post(uri.path, xml_data, {'Content-Type' => 'text/xml', 'Content-Length' => xml_data.length.to_s, 'Authorization' => "Basic #{base64user_and_pass}", "Connection" => "keep-alive" })
  puts res.body
bjhaid
  • 9,592
  • 2
  • 37
  • 47
  • Thanks bjhaid. Does it matter where in the script i put the xml_data? With the above script i'm getting an error: Authenticating... Authenticated... Sending XML data... electro-post.rb:23:in `
    ': undefined method `post' for # (NoMethodError)
    – nanotechz9l Dec 23 '13 at 20:15
  • @nanotechz9l it should appear before the post action – bjhaid Dec 23 '13 at 20:16
  • I keep getting method undefined errors for post and req with the code above. I placed it right before the post action as you mentioned. I'm using ruby version ruby 2.0.0p353 (2013-11-22). – nanotechz9l Dec 23 '13 at 20:27
  • @nanotechz9l modified the code to include whatever you missed the full code should work – bjhaid Dec 23 '13 at 20:32
  • added the basic auth portion – bjhaid Dec 23 '13 at 20:38
  • Yup that's similar to what I had but I still keep getting undefined method `basic_auth' error. The "req.basic_auth" code matches the ruby docs so dunno whats going on. – nanotechz9l Dec 23 '13 at 20:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43788/discussion-between-nanotechz9l-and-bjhaid) – nanotechz9l Dec 23 '13 at 22:16