HTTP Post是否被Cloud9阻止?

2024-05-20 19:53:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在cloud9ide上玩Python/Flask。到目前为止还挺有趣的。但当我尝试向我的测试项目添加httppost时,Flask返回403或500。据我所知,当我附加数据或发送POST方法时,“request”对象是None。但这没有道理。这是非常直接的,我能说的应该行得通。这是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

烧瓶运行正常。我可以点击一个geturl,返回数据就可以了。当我请求着陆时,我没有收到任何请求。在

谢谢


Tags: 数据对象方法fromnoneflaskrequestpost
1条回答
网友
1楼 · 发布于 2024-05-20 19:53:53

你有两个问题:

  • 你得到了500个错误

  • “something”总是没有

第一个问题是因为您没有从route函数返回任何内容。在

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

您可以通过在函数末尾添加return语句来解决这个问题。别忘了它需要一个字符串。在

^{pr2}$

第二个问题不是它看起来的样子。我怀疑对象不是None,但是返回字符串表示的函数返回None,所以打印出来的就是这个。请尝试print type(request)查看此操作。在

我想您想要访问的是form字段。下面是一个完整的例子:

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)

相关问题 更多 >