2

I'm trying to use Savon to make a SOAP request with Ruby, but I'm receiving a 400 Bad Request response from the server.

This is the request I'm trying to make (according to soapUI):

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:apis="http://www.csisoftwareusa.com/ApiService">    
    <soap:Header/>    
    <soap:Body>
      <apis:AuthenticateConsumer>
         <!--Optional:-->
         <apis:consumerName>?</apis:consumerName>
         <!--Optional:-->
         <apis:consumerPassword>?</apis:consumerPassword>
      </apis:AuthenticateConsumer>    
    </soap:Body> 
</soap:Envelope>

Here is the request that I make with Ruby; it returns a 400 Bad Request error:

<SOAP-ENV:Envelope>
  <SOAP-ENV:Body>
    <ins0:AuthenticateConsumer>
      <ins0:consumerName>?</ins0:consumerName>
      <ins0:consumerPassword>?</ins0:consumerPassword>
    </ins0:AuthenticateConsumer>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Http Headers: SOAPAction: "http://www.csisoftwareusa.com/ApiService/AuthenticateConsumer", Content-Type: text/xml;charset=UTF-8, Content-Length: 504

Here is the request that I was able to make with Python. THIS request succeeds:

<SOAP-ENV:Envelope>
  <SOAP-ENV:Header/>
  <ns0:Body>
    <ns1:AuthenticateConsumer>
      <ns1:consumerName>?</ns1:consumerName>
      <ns1:consumerPassword>?</ns1:consumerPassword>
    </ns1:AuthenticateConsumer>
  </ns0:Body>
</SOAP-ENV:Envelope>
Http headers: {'SOAPAction': u'"http://www.csisoftwareusa.com/ApiService/AuthenticateConsumer"', 'Content-Type': 'text/xml; charset=utf-8'}

I need to integrate calls to this API into a Rails application, so doing it in Python isn't a valid solution.

I'm wondering if anyone can see what I'm missing. Is the empty <SOAP-ENV:Header /> tag the issue, and if so, how can I add that to the Savon request?

Thanks, Stuart

Stuart
  • 644
  • 1
  • 6
  • 13
  • what is `>` in your ruby example? Seems like you have invalid xml. You should really build it using an xml builder like Nokogiri – Michael Papile Dec 14 '12 at 22:12
  • @MichaelPapile Apparently it was a typo in my formatting of the StackOverflow question; I reran the query and that last bracket isn't there. I've edited the text of the question to fix that. Thanks! – Stuart Dec 17 '12 at 13:46
  • any updates on this? I'm struggling with the same thing. Using version 2 of Savon Gem – hyeomans Jan 15 '13 at 18:38
  • @YeomansLeo It turns out that my issue is in the http headers; I posted a new question for that specifically. http://stackoverflow.com/questions/14482251/ruby-savon-soap-request-double-escapes-spaces-in-url – Stuart Jan 23 '13 at 17:10

2 Answers2

1

In my case, I had repeated namespaces, so I was getting 400 Bad Request.

My code:

require 'savon'

namespaces = {
  "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
  "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
}

client = Savon.client(
 ...
 :namespace => namespaces
}

I removed the :namespace option and the error was gone.

How did I find the error?

Use build_request instead of call and print the request body:

client.build_request(:search, message: {...})
puts request.body

I took the request body and pasted it into SoapUI, then I made changes one by one until the request was successful.

David
  • 97
  • 9
0

The issue here is with my http headers: Because the url has spaces in it, the url has to be encoded. However, I have to encode it before passing it to Savon, which then encodes it again - this double-encoded url fails. See: https://stackoverflow.com/questions/14482251/ruby-savon-soap-request-double-escapes-spaces-in-url

Thanks, Stuart

Community
  • 1
  • 1
Stuart
  • 644
  • 1
  • 6
  • 13