Ruby + Net::HTTP: How do I send two XML documents in one POST request?

时间:2022-12-01 16:26:41

I have to send two XML documents in my request to the UPS API (here's my original question What is the root of this XML document? )

我必须在请求中向UPS API发送两个XML文档(这是我原来的问题这个XML文档的根源是什么?)

How would I do this?

我该怎么办?

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 个解决方案

#1


0  

You should use MIME Multipart messages if the API support them (ruby gem). Otherwise just try to concatenate files' contents request.body = "#{xml_for_initial_request}\n#{second_xml_document}"

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

#1


0  

You should use MIME Multipart messages if the API support them (ruby gem). Otherwise just try to concatenate files' contents request.body = "#{xml_for_initial_request}\n#{second_xml_document}"

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