Changing GET to POST in Python (Flask)

不羁岁月 提交于 2020-01-16 05:16:08

问题


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.]


回答1:


You should not be using the POST method in this instance. The POST method is meant to be used when you are changing data on the server. In this case you are only retrieving data, so GET is the more appropriate method to use here.

In any case, if you do decide to use POST, your code is actually working. You can see this if you use a POST request to access the endpoint:

$ curl -X POST http://127.0.0.1:5000/
<div>
  [0.03464541692036849, 0.5588700799957625, 0.4702806873145451, 0.7525198710149907, 0.0674801622743858, 0.28229897849445273, 0.17400190415782735, 0.48911931330821357, 0.8033543541248421, 0.16335301905982258, 0.3307436416488905, 0.11670066397858725, 0.552907551276049, 0.6699689958218984, 0.7444295210533091, 0.8258885497774587, 0.8656500198078877, 0.6826827672886756, 0.27219907080455874, 0.9053285546116574, 0.8328655798090894, 0.2323223157770763, 0.9775485685217323, 0.34887389370958166, 0.17092511319368353, 0.20875570398480459, 0.6744092445507751, 0.6176283706166301, 0.05070680888843082, 0.3441890248079591, 0.17701427714228501, 0.115329649057473, 0.325114272097177, 0.19386610624431766, 0.18892384889766745, 0.511139418514318, 0.019931284111035397, 0.5240369332606203, 0.8936272011794374, 0.9665936114223397] 
</div>

If you just access the URL in your browser, this will actually send a GET request, and you have modified your code to specifically only allow the POST method - which is why you get the Method Not Allowed response.

$ curl -X GET http://127.0.0.1:5000/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

The best solution is for you to use GET in this instance.




回答2:


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.



来源:https://stackoverflow.com/questions/31095630/changing-get-to-post-in-python-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!