Is HTTP Post blocked by Cloud9?
Question: I've been playing around with Python/Flask on Cloud9 ide. Pretty fun so far.
But when I try to add a http post to my test project, Flask returns either a
403 or a 500. From what I can tell, when I attach data or send the POST
method, the 'request' object is None. It doesn't make sense though. This is
pretty straight forward and should work as far as I can tell. Here's the
python:

    
    
    from flask import Flask, jsonify, abort, request
    @app.route('/test', methods = ['POST'])
    def post():
        print ('started')
        print request
        if request.method == 'POST':
            something = request.get_json()
            print something
    

Flask is running correctly. I can hit a GET url, returning data just fine. I
get an error when I land on 'print request' because request is None.

Thanks,


Answer: You have two problems here:

  * You're getting a 500 error

  * "something" is always None

The first problem is because you're not returning anything from your route
function.

    
    
    127.0.0.1 - - [15/Dec/2014 15:08:59] "POST /test HTTP/1.1" 500 -
    Traceback (most recent call last):
      ...snip...
      ValueError: View function did not return a response
    

You can remedy this by adding a return statement at the end of the function.
Don't forget it needs to be a string.

    
    
    @app.route('/hi', methods = ['POST'])
    def post():
        return "Hello, World!"
    

The second problem isn't what it seems. I suspect that the object isn't None,
but the function that returns the string representation returns None, so
that's what gets printed. Try `print type(request)` to see this in action.

What I think you want access to is the `form` field. Here is a complete
example:

    
    
    from flask import Flask, request
    
    app = Flask(__name__) 
    
    @app.route('/test', methods = ['POST'])
    def post():
        print type(request)
        if request.method == 'POST':
            print request.form
        return str(request.form)
    
    app.run(debug=True)
    

