【问题标题】:Flask application not giving POST request from the browser URLFlask 应用程序未从浏览器 URL 发出 POST 请求
【发布时间】:2017-07-18 04:06:15
【问题描述】:

我在 Python3 中使用 POST 方法编写了一个 Flask 应用程序。每当我点击 URL 时,在命令提示符下,我都会看到 GET 方法被调用。
这是应用程序:

from flask import Flask, jsonify, make_response, request    
app = Flask(__name__)
@app.route('/api/v1.0/qanda/', methods=['POST'])
def people_api():
    if request.method == 'POST':
        text = request.data.get('text', '')

        if text is None:
           make_response(jsonify({'error': 'Missing text parameter'}), 400)
    return text
app.run()

每当我点击网址时:

http://127.0.0.1:5000/api/v1.0/qanda/?text=ggg

我在命令行中看到的是:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [18/Jul/2017 09:33:15] "GET /api/v1.0/qanda/?text=ggg HTTP/1.1" 200 -
127.0.0.1 - - [18/Jul/2017 09:33:22] "GET /api/v1.0/qanda/?text=ggg HTTP/1.1" 200 -

但我只想在点击 URL 时使用 post 方法。我不知道为什么 GET 方法正在发挥作用。请告诉我如何在通过浏览器访问 URL 时使用 POST 方法而不是 GET 方法。

【问题讨论】:

  • 用于创建 post 请求的 Chrome 插件(以及许多其他插件,例如 Postman)很不错。 <form method="post" ... > 的 html 表单也可以使用
  • 通过浏览器点击 URL 时,它总是会通过浏览器向您的服务器发送 GET 请求。你不可能通过这种方式发送 POST 请求!

标签: python python-3.x flask http-post flash-message


【解决方案1】:

您不能通过地址栏发出 POST 请求。您必须使用 JavaScript 或使用 method='post' 提交表单。

几个例子:

表格:

<form method='post' action='your url'>
  <input name='name' type='text'>
  <input type='submit'>
</form>

JavaScript(带有 jQ​​uery):

$.post({'your': 'data'})

【讨论】:

  • 但这不是地址栏。那是一个插件。除非我误解了它在做什么。
  • @CoryMadden 是的,你在写。我不是在寻找应用程序,而是在寻找地址栏的解决方案
  • 一个 html post 请求甚至没有在 url 中放入 args
【解决方案2】:

您无法从浏览器地址栏对网址进行发布调用。您可以使用postman 工具。使用此工具发起请求非常简单,支持所有请求方法和授权标头。

或者干脆你可以使用python的requests模块来提出请求

Import requests

r = requests.post('http://httpbin.org/post/', data = {'key':'value'})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2015-04-26
    • 1970-01-01
    • 2016-10-26
    • 2019-08-29
    相关资源
    最近更新 更多