문제

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
도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top