Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make AppleScript SOAP work with existing SOAP webservices (e.g. W3Schools' CelsiusToFarenheit example)?

I'm currently using AppleScript Editor. I run the following code there:

tell application "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"
    call soap {method name:"CelsiusToFahrenheit", method namespace uri:"http://www.w3schools.com/webservices/", parameters:{Celsius:50 as string}, SOAPAction:"http://www.w3schools.com/webservices/CelsiusToFahrenheit"}
end tell

If I execute the above code, I get an "Error" string back (which is not what I expected). The generated request is as follows:

<?xml version="1.0" encoding="utf-8"?>
  <SOAP-ENV:Envelope
    xmlns:xsd="http://www.w3.org/1999/XMLSchema"
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
      <SOAP-ENV:Body>
        <m:CelsiusToFahrenheit xmlns:m="http://www.w3schools.com/webservices/">
          <Celsius xsi:type="xsd:int">50</Celsius>
        </m:CelsiusToFahrenheit>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

If I edit the request like this (I used Fiddler to edit the request):

<?xml version="1.0" encoding="utf-8"?>
      <SOAP-ENV:Envelope
        xmlns:xsd="http://www.w3.org/1999/XMLSchema"
        xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
        SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
          <SOAP-ENV:Body>
            <CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
              <Celsius xsi:type="xsd:int">50</Celsius>
            </CelsiusToFahrenheit>
          </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
  1. Change "m:CelsiusToFarenheit" to "CelsiusToFarenheit"
  2. Change "xmlns:m" to "xmlns"

I receive "122" which is the correct response.

My questions are:

  1. Why is this happening? Is the SOAP format used by the "call soap" function an old format?
  2. Is there a way to fix this (e.g. remove ":m" or "m:" in the request or use different SOAP format through code by setting a parameter in the "call soap" method [i don't know if this is possible])?
like image 322
Dj S Avatar asked Oct 02 '22 08:10

Dj S


1 Answers

The skinny:

  • A bug in the call soap feature neglects to namespace-tag the parameters given to a call properly: the method name element is prefixed with m:, but the parameter elements are not.

  • The workaround is to explicitly prefix the parameter names with m:; in the above example, use |m:Celsius| instead of Celsius - note that the enclosing | signs are needed so you can use a name like this that would otherwise break parsing.

Caveat: There is a chance that this workaround will break should Apple ever fix the bug.

Applying the workaround to the above example gives us:

tell application "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"
  call soap {method name:"CelsiusToFahrenheit", ¬
            method namespace uri:"http://www.w3schools.com/webservices/", ¬
            parameters:{|m:Celsius|:50 as string}, ¬
            SOAPAction:"http://www.w3schools.com/webservices/CelsiusToFahrenheit"}
end tell

Background:

  1. (...) Is the SOAP format used by the "call soap" function an old format?

The short of it: AppleScript uses the SOAP 1.1 schema, and while the APIs under the hood also support SOAP 1.2, you cannot select it from AppleScript ((you can only do it from C using Carbon))

As described above, the SOAP XML construction has a bug, which is the reason the call didn't work. (The web service at http://www.w3schools.com/webservices/tempconvert.asmx does support 1.1, alongside 1.2).

Here are some clues regarding the importance and maintenance status of the call soap and call xml-rpc features:

  • The features are described in documentation separate from the AppleScript Language Guide, even though they appear to be part of the language itself (could not find them in any external dictionaries)

  • The documentation was written in 2001 (as of OSX 10.1; and has only seen a tiny revision in 2005; a warning of potentially outdate web-service URL was added :)).

'2. Is there a way to fix this (e.g. remove ":m" or "m:" in the request or use different SOAP format through code by setting a parameter in the "call soap" method?

(See the workaround above.)

Based on the documentation-status clues, I would not hold my breath for updates or fixes from Apple - which is a pity, because these features look really handy.

like image 149
mklement0 Avatar answered Oct 13 '22 11:10

mklement0