簡體   English   中英

Rails發布xml以保護Web服務api

[英]Rails post xml to secure web service api

我正在嘗試使用rails 4.0為Web服務創建一個API,該API需要直接向其安全服務器發送xml帖子。

我有一個提交`:action =>“testSubmit”的表單來觸發我的控制器中的以下代碼,經過數小時(大部分已過時)的研究和調試后,我將它拼湊在一起。

def testSubmit

  xml = Builder::XmlMarkup.new
  xml.instruct!
  xml.sale do
    xml.tag! 'tag-1', "INFO_HERE"
    xml.tag! 'tag-2', "MORE_INFO"
    xml.amount "100.00"
  end

  uri = URI('https://secure.abc.com')

  Net::HTTP.start(uri.host, uri.port,
    :use_ssl => uri.scheme == 'https') do |http|
    response = http.post("/api/some/path", xml.to_s)
    flash[:notice] = "Sent?"
  end    



end

rails框架確實重定向到testSubmit.html.erb視圖, flash存在,所以一切都表明它可能正在工作,但如果我嘗試讀取響應數據,它似乎是空的。 例如,如果我在視圖中包含<%= response %> ,我會看到:

#<ActionDispatch::Response:0x007fd167ddef60>

但是<%= response.body %>什么都沒有呈現......

我希望響應是特定的xml,我已經使用PHP和Curl驗證了,以及firefox附加組件“Poster”

解決了它。 下面的工作代碼。

 def testSubmit

      xml = Builder::XmlMarkup.new
      xml.instruct!
      xml.sale do
        xml.tag! 'tag-1', "INFO_HERE"
        xml.tag! 'tag-2', "MORE_INFO"
        xml.amount "100.00"
      end

      uri = URI('https://secure.abc.com')

      Net::HTTP.start(uri.host, uri.port,
        :use_ssl => uri.scheme == 'https') do |http|
        @xml = xml.target!
        @response = http.post(uri.path, @xml, initheader = {'Content-Type' =>'text/xml'})
        flash[:notice] = "Sent?"
      end    

 end

這個: <%= @response.body %>工作正常......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM