在Python(Flask)中将GET更改为POST [英] Changing GET to POST in Python (Flask)

查看:78
本文介绍了在Python(Flask)中将GET更改为POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的应用程序,其中在服务器上生成整数数组并将其发送给客户端.这是app.py中的一些示例代码(有效):

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)

这是abc.html的(有效)代码段:

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

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

问题是,此代码使用GET HTTP方法(当未指定时,Flask默认使用该方法).我想改用POST HTTP方法,因为它显然更安全地发送数据. (来源: http://blog.teamtreehouse.com/the-权威指南获取帖子)

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)

为此,我尝试更改:

@app.route('/')

收件人:

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

不幸的是,我收到一个错误:不允许使用方法:请求的URL不允许使用该方法."

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

问题:我该如何解决?

Question: How should I fix this?

[注意:据我目前的了解,我需要创建一个表单来使用POST方法,但是我认为我的网站不需要表单,因为客户端没有向服务器发送数据. (在我的网站上,服务器正在向客户端发送数据.)

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

推荐答案

在这种情况下,您不应使用POST方法. POST方法用于在服务器上更改数据时使用.在这种情况下,您仅要检索数据,因此GET是在此处使用的更合适的方法.

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.

无论如何,如果您决定使用POST,则您的代码实际上是有效的.如果您使用POST请求访问端点,则可以看到以下内容:

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>

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

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>

最好的解决方案是在这种情况下使用GET.

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

这篇关于在Python(Flask)中将GET更改为POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆