Ruby + Net :: HTTP:如何在一个POST请求中发送两个XML文档?

时间:2014-01-14 17:26:11

标签: ruby xml

我必须在请求中向UPS API发送两个XML文档(这是我原来的问题What is the root of this XML document?

我该怎么做?

def make_initial_request
  uri = URI.parse(UPS_API['confirm_url'])
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true       

  headers = {'Content-Type' => 'text/xml'}
  request = Net::HTTP::Post.new(uri.path, headers)
  request.body = xml_for_initial_request #<-- how do i split this into two documents?
  #request.body = second_xml_document #<-- i want something like that. could i just use << ?
  begin
    response = https.request(request)
  rescue
    return nil
  end
  puts "response: #{response.code} #{response.message}: #{response.body}"
  return nil if response.body.include?("Error")
end

1 个答案:

答案 0 :(得分:0)

如果API支持MIME Multipart消息(ruby gem),则应该使用它。 否则,只需尝试连接文件的内容request.body = "#{xml_for_initial_request}\n#{second_xml_document}"