title: Key Error when passing a variable using session in Flask
body: <p>I want to pass variables from a function to a class in a Flask app using <code>session</code>. This is my code:</p>
  
  <pre><code>@app.route('/login', methods=['POST'])
  def login():
      if not request.is_json:
          return jsonify({"msg": "Missing JSON in request"}), 400
  
      username = request.json.get('username', None)
      password = request.json.get('password', None)
      session['client_fname'] = request.json.get('Client First Name', None)
      session['client_lname'] = request.json.get('Client Last Name', None)
  
      ... ...
  
      access_token = create_access_token(identity=username)
      return jsonify(access_token=access_token), 200
  
  class PrivateResource(Resource):
      @app.route('/protected', methods=['GET'])
      @jwt_required
  
      def sendData():
          return mysqldb.addUser("{}".format(session['client_fname']),"{}".format(session['client_lname']))
  </code></pre>
  
  <p>The variables I want to pass are <code>session['client_fname']</code> and <code>session['client_lname']</code>. However, when I try to trigger <code>sendData()</code> using <code>curl -X GET http://localhost:5000/protected -H "Authorization: Bearer JWTGOESHERE"</code> I get:</p>
  
  <pre><code>  File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
      return self.wsgi_app(environ, start_response)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
      response = self.handle_exception(e)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask_restful/__init__.py", line 273, in error_router
      return original_handler(e)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
      response = self.full_dispatch_request()
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask_restful/__init__.py", line 273, in error_router
      return original_handler(e)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
      rv = self.dispatch_request()
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask_jwt_extended/view_decorators.py", line 101, in wrapper
      return fn(*args, **kwargs)
    File "/Users/open/PycharmProjects/FlaskMiddleware/Flask-API-Middleware-V1/authenticate.py", line 48, in sendData
      return mysqldb.addUser("{}".format(session['client_fname'],None),"{}".format(session['client_lname']))
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/werkzeug/local.py", line 377, in &lt;lambda&gt;
      __getitem__ = lambda x, i: x._get_current_object()[i]
    File "/Users/open/venv/FlaskMiddleware/lib/python2.7/site-packages/flask/sessions.py", line 83, in __getitem__
      return super(SecureCookieSession, self).__getitem__(key)
  KeyError: 'client_fname'
  </code></pre>
  
  <p>Whats wrong with the way I'm using <code>session</code> to pass the variables? How can I fix this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If this is a cookie issue having to do with preserving the session, how could I achieve this? Now I'm creating a cookie file in <code>curl -H "Content-Type: application/json" -X POST   -d '{"username":"user1","password":"abc123","Client First Name":"SAUR","Client Last Name":"KRIS"}' http://localhost:5000/login -c cookies.txt</code> and then trying <code>curl -X GET http://localhost:5000/protected -H "Authorization: Bearer JWTGOESHERE" -b cookies.txt</code>. Now I'm getting <code>TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.</code> and I'm not sure if this is progress or just plain wrong.  </p>