forked from railwayapp-templates/flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (27 loc) · 949 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
import os
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
# Handle GET request
return jsonify({"Choo Choo": "Welcome to your Flask app 🚅"})
@app.route("/test", methods=["POST"])
def test_post():
# Get the raw data from the request
data = request.get_data(as_text=True)
# Log the received data
print("Received POST data:")
print(data)
# Try to parse and pretty print JSON if possible
try:
json_data = json.loads(data)
print("Parsed JSON data:")
print(json.dumps(json_data, indent=2))
except json.JSONDecodeError:
print("Received data is not valid JSON")
# Return a success response
return jsonify({"status": "success", "message": "Data received and logged"}), 200
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(debug=True, host="0.0.0.0", port=port)