Created
September 30, 2025 11:00
-
-
Save ahmad88me/3ed3f915c4ccc9e65000a0b734c461dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app.py. Generated by chatGPT | |
| from flask import Flask, request, jsonify | |
| from pprint import pformat | |
| app = Flask(__name__) | |
| def collect_fields(req): | |
| data = {} | |
| # Query parameters (?foo=bar) | |
| if req.args: | |
| data["query_params"] = {k: req.args.getlist(k) if len(req.args.getlist(k)) > 1 else req.args.get(k) | |
| for k in req.args.keys()} | |
| # Form fields (application/x-www-form-urlencoded or multipart/form-data) | |
| if req.form: | |
| data["form_fields"] = {k: req.form.getlist(k) if len(req.form.getlist(k)) > 1 else req.form.get(k) | |
| for k in req.form.keys()} | |
| # JSON body (application/json) | |
| json_body = req.get_json(silent=True) | |
| if isinstance(json_body, dict): | |
| data["json"] = json_body | |
| # Files (just list filenames; reading file bytes is optional) | |
| if req.files: | |
| data["files"] = {k: req.files[k].filename for k in req.files.keys()} | |
| # A flat merged view (query + form + json, preferring JSON > form > query) | |
| merged = {} | |
| for section in ("query_params", "form_fields"): | |
| for k, v in data.get(section, {}).items(): | |
| merged.setdefault(k, v) | |
| if isinstance(json_body, dict): | |
| merged.update(json_body) # JSON wins | |
| data["merged_fields"] = merged | |
| return data | |
| @app.route("/", methods=["GET", "POST"]) | |
| def index(): | |
| method = request.method | |
| fields = collect_fields(request) | |
| # Print to server console | |
| print("=" * 60) | |
| print(f"Method: {method}") | |
| print("Fields:\n" + pformat(fields, width=100)) | |
| print("=" * 60) | |
| # Also return as JSON so you can see it in the client | |
| return jsonify({ | |
| "method": method, | |
| "fields": fields | |
| }), 200 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment