from flask import Flask, request, jsonify import requests app = Flask(__name__) @app.route('/') def home(): return "Welcome to the Flask URL Fetcher!" @app.route('/fetch', methods=['POST']) def fetch_url_content(): data = request.json target_url = data.get('url') if not target_url: return jsonify({"error": "No URL provided"}), 400 try: # Make the GET request to the target URL (raw Pastebin link) response = requests.get(target_url, headers={ "Cache-Control": "no-cache", "Pragma": "no-cache", "Accept": "text/plain" # Ensure we get the raw plain text response }) if response.status_code == 200: return jsonify({ "status_code": response.status_code, "content": response.text # Raw content from Pastebin }) else: # Return detailed error info if status code is not 200 return jsonify({ "error": "Failed to fetch content", "status_code": response.status_code, "response_text": response.text }), 500 except requests.exceptions.RequestException as e: # Capture and log any exception that occurs during the request return jsonify({"error": f"Request failed: {str(e)}"}), 500 # Prevent caching for responses from this Flask app itself @app.after_request def add_no_cache_headers(response): response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0" response.headers["Pragma"] = "no-cache" response.headers["Expires"] = "-1" return response if __name__ == "__main__": app.run(host='0.0.0.0', port=7860)