Changing GET to POST in Python (Flask)
Question: I am trying to create a simple app where an array of integers is generated on
the server and sent to the client. Here is some sample (working) code in
app.py:

    
    
    from flask import Flask, render_template, request, url_for
    
    import random
    
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def form():
        s_abc = [random.random() for _ in range(40)]
        return render_template('abc.html', s_abc=s_abc)
    
    if __name__ == '__main__':
      app.run(debug=True)
    

And here is a (working) snippet of abc.html:

    
    
    <div>
      {{s_abc}} 
    </div>
    

The problem is, this code uses the GET HTTP method (which Flask uses by
default when none is specified). I would like to instead use the POST HTTP
method, since it is apparently more secure for sending data. (Source:
<http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post>)

To do this, I tried to change:

    
    
    @app.route('/')
    

to:

    
    
    @app.route('/', methods=['POST'])
    

Unfortunately, I got an error: "Method Not Allowed: The method is not allowed
for the requested URL."

Question: How should I fix this?

[Note: From what I currently understand, I would need to create a form to use
the POST method, but I don't think my website would need a form, since the
client isn't sending data to the server. (In my website, the server is sending
data to the client.)]

[Note: This is similar to a question I asked previously, but not the same,
because here I am asking about a specific error message, Method not Allowed.]


Answer: When you use the browser to visit a page, the browser always send 'GET'
request to server... so when you've changed the methods to 'POST', flask can't
find any GET route for '/' and return "Method Not Allowed" error, as the '/'
doesn't allows GET anymore when browsers asks for that page.

You shouldn't use POST for this. POST only used when submitting data from
forms or ajax.

