joanna cheung
joanna cheung

Reputation: 161

iOS SOAP request for Mindbody API

I have been trying to make SOAP requests with swift for the past few days unsuccessfully and wondering if anybody can direct me to getting a successful response. I understand that NSURLConnection is deprecated but I wanted to test to see if I can even make a successful request since all examples that I can find online used it.

The soap request is directly off of their API web site at https://developers.mindbodyonline.com/, which free to signup.

let requestURL = NSURL(string: "https://api.mindbodyonline.com/0_5/SiteService.asmx")
let myRequest = NSMutableURLRequest(URL: requestURL!)

let soapMessage =
        "<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://clients.mindbodyonline.com/api/0_5\">" +
        "<soapenv:header />" +
        "<soapenv:body>" +
        "<GetLocations xmlns=\"http://clients.mindbodyonline.com/api/0_5\">" +
        "<request>" +
            "<sourcecredentials>" +
            "<sourcename>Siteowner</sourcename>" +
            "<password>apitest1234</password>" +
            "<siteids> <int>-99</int> </siteids>" +
            "</sourcecredentials>" +
            "<XMLDetail>Bare</XMLDetail>" +
             "<PageSize>10</PageSize>" +
             "<CurrentPageIndex>0</CurrentPageIndex>" +
             "<Fields>" +
                "<string>Locations.Name</string>" +
                "<string>Locations.City</string>" +
             "</Fields>" +
        "</request>" +
        "</GetLocations>" +
        "</soapenv:Body> </soapenv:envelope>"


let sMessageLength = NSString(format: "%d", soapMessage.characters.count)
myRequest.addValue("text/xml; charset=UTF-8", forHTTPHeaderField: "Content-Type")
myRequest.addValue(sMessageLength as String, forHTTPHeaderField: "Content-Length")
myRequest.HTTPMethod = "POST"
myRequest.addValue("http://clients.mindbodyonline.com/api/0_5/GetLocations", forHTTPHeaderField: "SOAPAction")
myRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)

 sConnection = NSURLConnection(request: myRequest, delegate: self)

I've tried many things but most of the time I get the following error response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 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">
<soap:Body>
<soap:Fault>
    <faultcode>soap:Client</faultcode>
    <faultstring>Server was unable to read request. ---&gt; Request format is invalid: Missing required soap:Envelope element.
    </faultstring><detail />
</soap:Fault></soap:Body></soap:Envelope>

I cannot figure out what I am doing wrong since this code is directly off their web site example.

Upvotes: 0

Views: 383

Answers (2)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

Try changing the tags that you have for the SourceCredentials to use CamelCase:

"<SourceCredentials>" +
"<SourceName>Siteowner</SourceName>" +
"<Password>apitest1234</Password>" +
"<SiteIDs> <int>-99</int> </SiteIDs>" +
"</SourceCredentials>" +

Upvotes: 0

Evan
Evan

Reputation: 23

Going off of the XML, it looks like you're passing through the UserCredentials used to log in to the API Sandbox as your SourceCredentials.

Try passing through your API Credentials listed on their developer portal and let us know if you still get this error.

Upvotes: 0

Related Questions