So...the story in the nutshell is that I had this all working using a Apache webserver running locally (using PHP as the backend). I then wanted to get it running on a real web server and thought that Google App Engine would be nice. I got it working for the most part (local GAE app, with Cloud SQL) but could not get connection to Google Cloud Storage. It seemed to be PHP. And not much help in the community (move_uploaded_file returns TRUE but no file in GCS) I decided I am going to try and switch over to Python and give that a shot.
I am still very new to python, http, requests, php, etc so please be gracious ;) but I can't even get the basic python script to work with the iOS application. I am getting a HTTP error 404. (the php version worked)
Here is all the code:
objective-C:
...
//Form the post with the username and password
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[self.usernameTextField text],[self.passwordTextField text]];
NSURL *url=[NSURL URLWithString:@"http://localhost:9080/login.py"];
//Encapsulate the post data
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//Get the post length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
//Form the URL request with postData and url
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
//send the request to see if username and password are valid
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
app.yaml
application: app2
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: login.application
libraries:
- name: webapp2
version: latest
login.py
import webapp2
from webapp2_extras import json
class Login(webapp2.RequestHandler):
def post(self):
user_email = self.request.get('email')
#return JSON
self.response.content_type = 'application/json'
error_message = "email = %s" % user_email
obj = {
'success': 0,
'error_message': error_message,
}
self.response.write(json.encode(obj))
json.dumps(json.encode(obj))
application = webapp2.WSGIApplication([
('/', Login)
], debug=True)
def main():
application.run()
if __name__ == "__main__":
main()
old login.php
<?php
header('Content-type: application/json');
if($_POST) {
//Get user's information
$user_email = strip_tags(trim(strtolower($_POST['email'])));
echo json_encode(array('success' => 0,'error_message' => "email = $user_email"));
}
?>