Send http-request with Optional("") value - swit to php

Hi I have a problem like this, I'm writing a swift application 3 that communicates with a php file on a remote server, my problem is that when I send the parameters in post to the http page, the server receives the parameters incorrectly , In the request to the php file comes a constructed string like this: Optional ("prova@email.it") , Should theoretically only arrive: prova@email.it, how do I fix this problem? Placed below both the php and swift codes


SWIFT CODE:

import Foundation
class User{

    /
    let URL_SAVE_TEAM = "http://mylink/myfile.php"
    var email=""
    var password=""

    func PrintValue(){
     
       /
        /
    }

    func Login() -> String{

     
     
        var ris="";
        /
        guard let requestURL = URL(string: URL_SAVE_TEAM) else { return "no" }
     
        /
        var request = URLRequest(url: requestURL)
     
        /
        request.httpMethod = "POST"
     
        /
     
     
        /
        let postParameters = "email=\(email)&password=\(password)"
     
        /
        request.httpBody = postParameters.data(using: .utf8)
     
        /
        let session = URLSession.shared
     
        let task = session.dataTask(with: request) {
            data, response, error in
         
            guard error == nil else {
                print("error is \(error!.localizedDescription)")
                return
            }
         
            guard let data = data else {
                print("No data was returned by the request!")
                return
            }
         
            /
            do {
                /
                let myJSON =  try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Dictionary<String, String?>
             
                /
                guard let parseJSON = myJSON, let msg = parseJSON["message"] as? String else {
                    print("Error parsing data")
                    return
                }
             
                /
                print(msg)
             
                ris=msg;
             
            } catch {
                print(error)
            }
         
        }
     
        /
        task.resume()
     
     
        return ris;
    }


}


PHP CODE:

<?php


include 'user.php';


header('Content-Type: application/json');
$email= $_POST['email'];
$password = $_POST['password'];


$ris['message']="";


$user = new User();


//procedo con il login
if($user->login($email,$password,"MOBILE")==true){
    $ris['message']="yes";
}




else{
  $ris['message']="no $email";
}


echo json_encode($ris);


?>

It’s hard to be sure without seeing more of your code but problems like this are usually related to the use of string interpolation. For example:

let email: String? = "mrgumby@frog.com"
let password = "opendoor"
let postParameters = "email=\(email)&password=\(password)"  
print(postParameters)   // -> email=Optional("mrgumby@frog.com")&password=opendoor

In your case the

email
property is not optional, so that’s not the immediate cause of this problem. It’s likely that the issues lies with whoever is setting the
email
property.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Send http-request with Optional("") value - swit to php
 
 
Q