简体   繁体   English

在Python(Flask)中将GET更改为POST

[英]Changing GET to POST in Python (Flask)

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: 这是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: 这是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). 问题是,此代码使用GET HTTP方法(当未指定时,Flask默认使用该方法)。 I would like to instead use the POST HTTP method, since it is apparently more secure for sending data. 我想改用POST HTTP方法,因为它显然更安全地发送数据。 (Source: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post ) (来源: 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." 不幸的是,我得到一个错误:“不允许使用方法:请求的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. [注意:据我目前的了解,我需要创建一个表单来使用POST方法,但是我认为我的网站不需要表单,因为客户端没有向服务器发送数据。 (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.] [注意:这与我之前询问的问题类似,但不相同,因为在这里我询问的是特定的错误消息,方法不允许。

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. 当您使用浏览器访问页面时,浏览器总是向服务器发送“ GET”请求...因此,当您将方法更改为“ POST”时,flask找不到“ /”的任何GET路由并返回“方法不允许”错误,因为当浏览器要求该页面时,“ /”不再允许GET。

You shouldn't use POST for this. 您不应该为此使用POST。 POST only used when submitting data from forms or ajax. POST仅在从表单或Ajax提交数据时使用。

You should not be using the POST method in this instance. 在这种情况下,您不应使用POST方法。 The POST method is meant to be used when you are changing data on the server. POST方法用于在服务器上更改数据时使用。 In this case you are only retrieving data, so GET is the more appropriate method to use here. 在这种情况下,您仅要检索数据,因此GET是在此处使用的更合适的方法。

In any case, if you do decide to use POST, your code is actually working. 无论如何,如果您决定使用POST,则您的代码实际上是有效的。 You can see this if you use a POST request to access the endpoint: 如果使用POST请求访问端点,则可以看到以下内容:

$ 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. 如果仅在浏览器中访问URL,则实际上将发送GET请求,并且您已经修改了代码以专门仅允许POST方法-这就是为什么得到Method Not Allowed响应的原因。

$ 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. 最好的解决方案是在这种情况下使用GET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM