""" id: http title: HTTP Client (unsafe) author: admin description: Make HTTP requests (GET/POST) with headers and body. version: 0.1.0 license: Proprietary """ import subprocess import json ARTIFACTS_DIR = "/data/adaptai/artifacts" class Tools: def get(self, url: str, headers_json: str = "{}") -> dict: try: headers = json.loads(headers_json) hflags = " ".join([f"-H '{k}: {v}'" for k, v in headers.items()]) cmd = f"curl -sSL -D - {hflags} '{url}'" p = subprocess.run( ["bash", "-lc", cmd], capture_output=True, text=True, timeout=60 ) import os import time os.makedirs(ARTIFACTS_DIR, exist_ok=True) out = { "stdout": p.stdout, "stderr": p.stderr, "exit_code": p.returncode, "cmd": cmd, } if len(out.get("stdout", "")) > 4000: fn = f"{ARTIFACTS_DIR}/http_{int(time.time())}.log" open(fn, "w", encoding="utf-8").write(out["stdout"]) out["stdout_file"] = fn out["stdout"] = out["stdout"][:4000] + "... [saved]" return out except Exception as e: return {"error": str(e)} def post(self, url: str, body: str = "", headers_json: str = "{}") -> dict: try: headers = json.loads(headers_json) hflags = " ".join([f"-H '{k}: {v}'" for k, v in headers.items()]) cmd = f"curl -sSL -D - -X POST {hflags} --data '{body.replace("'", "'''")}' '{url}'" p = subprocess.run( ["bash", "-lc", cmd], capture_output=True, text=True, timeout=60 ) import os import time os.makedirs(ARTIFACTS_DIR, exist_ok=True) out = { "stdout": p.stdout, "stderr": p.stderr, "exit_code": p.returncode, "cmd": cmd, } if len(out.get("stdout", "")) > 4000: fn = f"{ARTIFACTS_DIR}/http_{int(time.time())}.log" open(fn, "w", encoding="utf-8").write(out["stdout"]) out["stdout_file"] = fn out["stdout"] = out["stdout"][:4000] + "... [saved]" return out except Exception as e: return {"error": str(e)}