无法从我的烧瓶服务器发出 POST 请求

时间:2020-12-22 10:26:34

标签: python flask python-requests

我正在尝试向我的 Flask 服务器发出 POST 请求,该服务器是使用带有 Gunicorn 和 Nginx 的谷歌计算引擎部署的。我可以做一个 get 请求,但我不能做一个 POST 请求,这使得它有点无用。

我的烧瓶代码:

from flask import *
import os
from nlp import generate
# initialize
app = Flask(__name__)

# home-page
@app.route("/", methods=["POST", "GET"])
def home():
    if request.method == 'GET':
        return render_template("index.html", txt="Empty")
    else:
        start = request.form["start"]
        generated_txt, sans =  generate(start)
        return render_template("index.html", txt=generated_txt, sans = sans)

@app.route('/favicon.ico')
def favicon():
    return app.send_static_file('favicon.ico')

@app.route('/download/corpus')
def download():
    return send_file('corpus.txt', as_attachment=True, attachment_filename='corpus.txt')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)

我的 HTML 代码与表单:

{%extends "base.html" %}
{% block title %} VedaLearn {% endblock %}
{% block style %} <link rel="stylesheet" href="{{ url_for('static', filename='styles/main.css') }}" /> {% endblock %}
{% block content %}
<body>
  <div class="container">
    <div class="row">
      <div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
        <div class="card card-signin my-5">
          <div class="card-body">
            <h5 class="card-title text-center">Prompt</h5>
            <form class="form-signin" method = "POST">
              <div class="form-label-group">
                <input type="text" name="start" id="inputEmail" class="form-control" placeholder="mahabharat" required autofocus>
                <label for="inputEmail">Text in IAST</label>
              </div>
              <button class="btn btn-lg btn-primary btn-block text-uppercase" value="Submit" type="submit">Generate</button>
              <hr class="my-4">
            </form>
            {% if txt != "Empty"%}
        <b>IAST:</b> {{txt}}<br><br><b>Sanskrit:</b> {{sans}}<br><br>
        {%endif%}
        <a href="https://github.com/Vaibhav2001/VedaLearn" target="_blank">View Github</a><br>
        <a href="download/corpus" target="blank">Download the corpus</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
{% endblock %}

这是我用于测试的python代码:

import json
import requests
api_url = 'http://vedalearn.me'
create_row_data = {'start': 'hello'}
get_r = requests.get(url=api_url)
post_r = requests.post(url=api_url, json=create_row_data)

get 有效,但 POST 无效,我不明白为什么!我最终必须将此逻辑扩展到我正在制作的 iOS 应用程序,因此我试图让这个 POST 方法正常工作。顺便说一句,如果你去 http://vedalearn.me 那里有一个表格,一切都按预期工作。任何帮助,将不胜感激。谢谢!

0 个答案:

没有答案