Cory Sparks
Cory Sparks

Reputation: 255

Sending string from Node.js (Express) server to iOS Swift 3

I am trying to create a login system for my iOS mobile application. I have a request sending to my Node.js server with Swift 3 with:

@IBAction func loginBtn(_ sender: UIButton) {

    //created NSURL
    let requestURL = NSURL(string: loginURL)

    //creating NSMutableURLRequest
    let request = NSMutableURLRequest(url: requestURL! as URL)

    //setting the method to post
    request.httpMethod = "POST"

    //getting values from text fields
    let empNum = empTextField.text
    let empPass = passTextField.text

    //creating the post parameter by concatenating the keys and values from text field
    let postParameters = "empNum=" + empNum! + "&empPass=" + empPass!;

    //adding the parameters to request body
    request.httpBody = postParameters.data(using: String.Encoding.utf8)


    //creating a task to send the post request
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in

        if error != nil{
            print("error is \(String(describing: error))")
            return;
        }

        //parsing the response
        do {
            //converting resonse to NSDictionary
            let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

            print("myjson : \(String(describing: myJSON))")

            //parsing the json
            if let parseJSON = myJSON {

                //creating a string
                var msg : String!

                //getting the json response
                msg = parseJSON["message"] as! String?

                //printing the response
                print("message here: \(msg)")

            }
        } catch {
            print("Error here: \(error)")
        }

    }
    //executing the task
    task.resume()

}

I am getting a 200 server side but I want to be able to res.send a string back to Swift so I can proceed to take further actions. How do I check that response? Like:

if response == "vaild" {
    ...
}else{
    ...
}

When I run this code I get an error that reads:

Error here: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

P.S. I am also not really familiar with the request code so if it is right in front of my face please just explain the code to me then. Sorry in advance!!

Upvotes: 0

Views: 736

Answers (1)

lpdavis13
lpdavis13

Reputation: 228

Your code sample suggests that your client expects a json response with a property message in it. If you are using resp.send('Success') from your server it will not be a json object. It will be a string.

If that is what you want on your server response you should update your client to simply parse the data to a string. You can use String(data: data, encoding: .utf8) where "data" is what is returned from the response.

However, I would recommend leveraging HTTP Status codes. In your server code you can respond with a 422 if login was not successful. This can be accomplished with resp.status(422).send('Some optional message'). Then client side all you need to do is check the response status. Instead of a string compare.

Upvotes: 1

Related Questions