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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

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 <main>': undefined method post' for #<Net::HTTP::Post POST> (NoMethodError)
@nanotechz9l it should appear before the post action
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 modified the code to include whatever you missed the full code should work
added the basic auth portion
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.