title: iOS Post to Python Script on Google App Engine
body: <p>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 (<a href="https://stackoverflow.com/questions/24830388/move-uploaded-file-returns-true-but-no-file-in-gcs">move_uploaded_file returns TRUE but no file in GCS</a>)
  I decided I am going to try and switch over to Python and give that a shot. </p>
  
  <p>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)</p>
  
  <p>Here is all the code:</p>
  
  <p>objective-C:</p>
  
  <pre><code>...
  //Form the post with the username and password
  NSString *post =[[NSString alloc] initWithFormat:@"email=%@&amp;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:&amp;response error:&amp;error];
  </code></pre>
  
  <p>app.yaml</p>
  
  <pre><code>application: app2
  version: 1
  runtime: python27
  api_version: 1
  threadsafe: true
  
  handlers:
  - url: /.*
    script: login.application
  
  libraries:
  - name: webapp2
    version: latest
  </code></pre>
  
  <p>login.py</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>old login.php</p>
  
  <pre><code>&lt;?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' =&gt; 0,'error_message' =&gt; "email = $user_email"));
      }
  ?&gt;
  </code></pre>