Emre Önder
Emre Önder

Reputation: 2537

Error Code 500 with Soap Request in Swift

I'm trying to do a soap request which calls encrypt function and returns a value. When I do the request on postman, It works. However, I got the code from postman but It returns error code 500. I think problem can be in postData, I add \ before double quotes. Code:

        let headers = [
        "content-type": "text/xml",
        "cache-control": "no-cache",
        "postman-token": "4d266611-ea55-0c8a-7879-eac8bf387ed0"
    ]

    let postData = NSData(data: "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><Encrypt xmlns=\"http://tempuri.org/\"><request>RequestIsValid29:01:2015 16:31</request> </Encrypt></soap:Body> </soap:Envelope>".data(using: String.Encoding.utf8)!)

    let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.httpBody = postData as Data

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse)
        }
    })

    dataTask.resume()

Error Code:

    <NSHTTPURLResponse: 0x608000222300> { URL: http://mobileexam.veripark.com/mobileforeks/service.asmx } { status code: 500, headers {
    "Cache-Control" = private;
    Connection = "Keep-Alive";
    "Content-Length" = 1802;
    "Content-Type" = "application/soap+xml; charset=utf-8";
    Date = "Mon, 28 Aug 2017 14:57:47 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} })

Upvotes: 1

Views: 585

Answers (2)

Emre &#214;nder
Emre &#214;nder

Reputation: 2537

As I mentioned in the question. Problem was that xml part in string. I add \ before " and also \r when needs to new line. Below part is the right string version of XML.

"\rhttp://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\rhttp://tempuri.org/\">\rRequestIsValid29:01:2015 16:31 \r \r"

So the final working code for question is:

let poststring = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r<soapenv:Header/> <soapenv:Body>\r<tem:GetForexStocksandIndexesInfo> <tem:request>\r<tem:IsIPAD>true</tem:IsIPAD>\r<tem:DeviceID>test</tem:DeviceID>\r<tem:DeviceType>ipad</tem:DeviceType>\r<tem:RequestKey>\(self.encrypt_code!)</tem:RequestKey>\r<tem:RequestedSymbol>\(name)</tem:RequestedSymbol>\r<tem:Period>Month</tem:Period> </tem:request>\r</tem:GetForexStocksandIndexesInfo> </soapenv:Body>\r</soapenv:Envelope>"
let postData = NSData(data: poststring.data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://mobileexam.veripark.com/mobileforeks/service.asmx")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)

Upvotes: 2

Vojta
Vojta

Reputation: 900

Did you try?

[lobj_Request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

Upvotes: 1

Related Questions