2009-07-26 58 views
5

我想向SOAP Web Service發出請求,但我不想安裝任何gem。 有沒有什麼辦法可以使用普通的XML進行請求?在Rails中使用XML進行SOAP請求

我認爲這是微不足道的,但可能有一些我錯過了,因爲所有的實現/教程都使用了寶石。

我認爲SOAP響應也可以作爲XML響應來處理嗎?

請求是這樣的:

POST /services/tickets/issuer.asmx HTTP/1.1 
Host: demo.demo.com 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Body> 
    <Tick xmlns="http://demo.com/test/test"> 
     <Request> 
     <Username>string</Username> 
     <Password>string</Password> 
     <AcquirerId>int</AcquirerId> 
     <RequestType>string</RequestType> 
     <ExpirePreauth>unsignedByte</ExpirePreauth> 
     <BitPerSec>int</BitPerSec> 
     <Office>string</Office> 
     </Request> 
    </Tick> 
    </soap12:Body> 
</soap12:Envelope> 

回答

6

你可以這樣做:

def post_xml(path, xml) 
    host = "http://demo.demo.com" 
    http = Net::HTTP.new(host) 
    resp = http.post(path, xml, { 'Content-Type' => 'application/soap+xml; charset=utf-8' }) 
    return resp.body 
end 

XML響應會通過此方法返回。

+0

使用Ruby 1.9.2和Rails 3.2.12 - 我不得不從主機名中刪除「http://」。另外,使用SOAP 1.1並且必須將'Content-Type'設置爲'text/xml`謝謝! – Josh 2014-01-20 17:32:52