Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/client/cli/commands/http.py: 0%
47 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
1import json as json_lib
2from typing import Final
4import click
5import requests
6import rich
8from ...http_client import HTTPClient
11@click.group()
12def http():
13 """Make HTTP requests to the LiteLLM proxy server"""
16@http.command()
17@click.argument("method")
18@click.argument("uri")
19@click.option(
20 "--data",
21 "-d",
22 type=str,
23 help="Data to send in the request body (as JSON string)",
24)
25@click.option(
26 "--json",
27 "-j",
28 type=str,
29 help="JSON data to send in the request body (as JSON string)",
30)
31@click.option(
32 "--header",
33 "-H",
34 multiple=True,
35 help="HTTP headers in 'key:value' format. Can be specified multiple times.",
36)
37@click.pass_context
38def request(
39 ctx: click.Context,
40 method: str,
41 uri: str,
42 data: str | None = None,
43 json: str | None = None,
44 header: tuple[str, ...] = (),
45):
46 """Make an HTTP request to the LiteLLM proxy server
48 METHOD: HTTP method (GET, POST, PUT, DELETE, etc.)
49 URI: URI path (will be appended to base_url)
51 Examples:
52 litellm http request GET /models
53 litellm http request POST /chat/completions -j '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}'
54 litellm http request GET /health/test_connection -H "X-Custom-Header:value"
55 """
56 # Parse headers from key:value format
57 headers: Final = {}
58 for h in header:
59 try:
60 key, value = h.split(":", 1)
61 headers[key.strip()] = value.strip()
62 except ValueError:
63 raise click.BadParameter(f"Invalid header format: {h}. Expected format: 'key:value'")
65 # Parse JSON data if provided
66 json_data = None
67 if json:
68 try:
69 json_data = json_lib.loads(json)
70 except ValueError as e:
71 raise click.BadParameter(f"Invalid JSON format: {e}")
73 # Parse data if provided
74 request_data = None
75 if data:
76 try:
77 request_data = json_lib.loads(data)
78 except ValueError:
79 # If not JSON, use as raw data
80 request_data = data
82 client: Final = HTTPClient(ctx.obj["base_url"], ctx.obj["api_key"])
83 try:
84 response: Final = client.request(
85 method=method,
86 uri=uri,
87 data=request_data,
88 json=json_data,
89 headers=headers,
90 )
91 rich.print_json(data=response)
92 except requests.exceptions.HTTPError as e:
93 click.echo(f"Error: HTTP {e.response.status_code}", err=True)
94 try:
95 error_body: Final = e.response.json()
96 rich.print_json(data=error_body)
97 except json_lib.JSONDecodeError:
98 click.echo(e.response.text, err=True)
99 raise click.Abort()