0

Whenever I send a HTTP request from my Swift app to my local server, it always responds with a 500 status, an empty Optional() body, and a text/html Content-Type. I'm new to using HTTP in this way, so I am unsure where the error might be. I have installed pecl-http and (I think) I have it properly configured. I also (think) that I am setting the body and content type properly in the PHP code. I followed the Swift part of this tutorial for my Swift code and created the PHP myself; I just have no idea where the problem is. I think my error is in sending/receiving the response, so that's the code I have attached.

HTTP Response

Response: <NSHTTPURLResponse: 0x7f8b44828b80> { URL: http://localhost/practicejournal/includes/php/json_login.php } { status code: 500, headers {
    Connection = close;
    "Content-Length" = 0;
    "Content-Type" = "text/html";
    Date = "Sun, 21 Dec 2014 17:12:53 GMT";
    Server = "Apache/2.4.9 (Unix) PHP/5.5.14";
    "X-Powered-By" = "PHP/5.5.14";
} }
Body: Optional()
The operation couldn’t be completed. (Cocoa error 3840.)
Error could not parse JSON: 'Optional()'

Swift Code Receiving Response

var task = session.dataTaskWithRequest(request, completionHandler: {data, response,     error -> Void in
        println("Response: \(response)")
        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("Body: \(strData)")
        var err: NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary
        if(err != nil) {
            println(err!.localizedDescription)
            let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("Error could not parse JSON: '\(jsonStr)'")
        } else {
            if let parseJSON = json {
                var success = parseJSON["success"] as? Int
                println("Success \(success)")
            } else {
                let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
            }
        }
    })

PHP Code Sending Response

//If Login is successful
$conn = null;
$success = 1;
HttpResponse::status(200);
HttpResponse::setContentType("application/json");
HttpResponse::setData($success);
HttpResponse::send();
//If Login is unsuccessful
function loginError() {
    $conn = null;
    $sucsess = 0;
    HttpResponse::status(200);
    HttpResponse::setContentType("application/json");
    HttpResponse::setData($success);
    HttpResponse::send();
}
Blake Morgan
  • 767
  • 1
  • 7
  • 25

2 Answers2

1

Turn on error reporting in PHP. See How do I get PHP errors to display?

Clearly you just have a mistake which is preventing your PHP from running correctly, but PHP is configured to not send you the error information. Clearly the $sucsess reference is wrong, but there could be other issues, too. (You've only shared a bit of the PHP, so have no way of knowing what else might be lurking out there.) By changing php.ini to show errors, you won't have to guess what the source of the problem is.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

There are multiple steps I took to get the code to work:

1) Wrong pecl_http Version: I had v2, which doesn't support the functions in the PHP HTTP Extension Documentation. I had to download and install v1 to use the functions in the documentation.

2) Not Processing POST Request Properly: Once I got the connection working, I needed to fetch the request body, decode it, and assign it to the $username and $password variables.

$body = http_get_request_body();
$body = json_decode($body);
$username = $body->{"username"};
$password = $body->{"password"};

3) Not Sending Response Properly: Rather than calling all those methods for the HttpResponse, I only needed to set the Content-Type and then echo $success.

HttpResponse::setContentType("application/json");
echo $success;
Blake Morgan
  • 767
  • 1
  • 7
  • 25