firefly
firefly

Reputation: 201

Error when making post request to server in Swift

I'm relatively new to iOS development. Currently, I'm following the tutorial on making POST request to server in Swift. However, I'm getting error messages that I don't really understand what is wrong with it.

    2016-01-08 14:44:48.991 test[24331:4311765] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
error=Optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSUnderlyingError=0x7c28c9b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorCodeKey=-9802, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7ae43c60>, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7c28aad0 [0x4ef098]>{type = immutable, count = 1, values = (
    0 : <cert(0x7c191330) s: localhost i: localhost>
)}}}, _kCFStreamErrorCodeKey=-9802, NSErrorFailingURLStringKey=https://localhost/, NSErrorPeerCertificateChainKey=<CFArray 0x7c28aad0 [0x4ef098]>{type = immutable, count = 1, values = (
    0 : <cert(0x7c191330) s: localhost i: localhost>
)}, NSErrorClientCertificateStateKey=0, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7ae43c60>, NSErrorFailingURLKey=https://localhost/})

POST request in Swift:

let myUrl = NSURL(string: "https://localhost");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
let postString = "firstName=James&lastName=Bond";

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

            // Print out response body
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")

            //Let’s convert response sent from a server side script to a NSDictionary object:

            do {
                let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

                // YOUR CODE HERE
                if let parseJSON = myJSON {
                    // Now we can access value of First Name by its key
                    let firstNameValue = parseJSON["firstName"] as? String
                    print("firstNameValue: \(firstNameValue)")
                }

            } catch {
                print(error)
            }
 }

 task.resume()

Code in index.php:

<?php
// Read request parameters
 $firstName= $_REQUEST["firstName"];
 $lastName = $_REQUEST["lastName"];// Store values in an array
 $returnValue = array(“firstName”=>$firstName, “lastName”=>$lastName);
// Send back request in JSON format
 echo json_encode($returnValue); 
?>

Upvotes: 0

Views: 549

Answers (1)

Mihado
Mihado

Reputation: 1547

Your localhost is not https, it's http. In iOS 9 you'll have to disable that in your info.plist so it allows you to make requests to non-https targets.

Add this to your info.plist enter image description here

Upvotes: 1

Related Questions