gpt4 book ai didi

ruby - 在使用 Ruby net/http 将 xml 属性列表作为 HTTP 正文发送时需要帮助

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:54 24 4
gpt4 key购买 nike

但是,下面的脚本有效,我收到了 400“错误请求”响应,因为 POST 必须将以下 XML 数据包含到 HTTP 正文中。我一直在尝试不同的方法来完成这项工作但没有成功。有人能指出我正确的方向吗?

XML 属性文件:

<?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 代码:

#!/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

最佳答案

使用下面的:

   #!/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

关于ruby - 在使用 Ruby net/http 将 xml 属性列表作为 HTTP 正文发送时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20750253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com