[Python] 404 when try to post some text into my webserver with python and flask

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am still very new to Python, just know some basic stuff bout it. I have built a small web application that takes input from a web server and sends it to the backend, which stores everything in a database. Everything is already running so far (via Docker), but when I want to make an entry, I get the following error message back (i guess my webserver got some issues):

    [![404 post issue][1]][1]

    i went through frontend to backend and looked over my post method - but it seems ok for me. Or am i blind to see the issue?

    I will post the Code below, so u can see what i mean :)

    <body>
    <h1>Willkommen in der Test App</h1>
    <form action="/api/data" method="POST">
    <label for="inputText">Text eingeben:</label>
    <input
    type="text"
    id="inputText"
    name="inputText"
    placeholder="z.B. Hallo Welt"
    />
    <button type="submit">Absenden</button>
    </form>


    Middleware Code:

    from flask import Flask, request, jsonify

    app = Flask(__name__)

    @app.route('/categorize', methods=['POST'])
    def categorize():
    data = request.get_json()
    input_text = data.get("inputText", "").lower()
    if "error" in input_text:
    category = "Error"
    else:
    category = "General"

    return jsonify({"category": category}), 200

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


    And the POST method in my Backend Code:

    @app.route('/api/data', methods=['POST'])
    def receive_data():
    input_text = request.form.get('inputText')
    if not input_text:
    return "Fehlender 'inputText' im Formular.", 400

    try:
    # An Middleware schicken zur Kategorisierung
    response = requests.post("http://middleware:6000/categorize",
    json={
    "inputText": input_text
    })
    if response.status_code != 200:
    return "Fehler in Middleware-Antwort", 500

    category = response.json().get("category", "Unkown")

    conn = psycopg2.connect(**db_config)
    cur = conn.cursor()
    insert_query = "INSERT INTO requests (text, category) VALUES
    (%s, %s);"
    cur.execute(insert_query, (input_text, category))
    conn.commit()
    cur.close()
    conn.close()

    return f"Eingegangen und gespeichert: '{input_text}' mit
    Kategorie '{category}'", 200

    except Exception as e:
    return f"Fehler bei der Verarbeitung: {str(e)}", 500


    my url after the post is: http://localhost/api/data

    Hope someone can give a nice hint to fix it :)

    Continue reading...

Compartilhe esta Página