Skip to main content

forward_request

Function forward_request 

Source
pub async fn forward_request(
    client: &Client,
    upstream_url: &str,
    path: &str,
    body: &Value,
    headers: Vec<(String, String)>,
    timeout: Duration,
) -> Result<Response, GuardrailError>
Expand description

Forward a (possibly redacted) request body to the upstream LLM API.

§Arguments

  • client — a shared reqwest::Client (connection-pooled).
  • upstream_url — base URL of the upstream, e.g. https://api.openai.com.
  • path — the request path and query string, e.g. /v1/chat/completions or /openai/deployments/prod/chat/completions?api-version=2024-10-21.
  • body — the JSON body to send (see crate::translate::ScannedBody::apply).
  • headers — headers from the original client request to forward (see forwardable_headers).
  • timeout — per-request timeout.

§Errors

Returns GuardrailError::Upstream if the request fails to send or the connection is refused/times out.

§Examples

use guardrail_proxy::forward::forward_request;
use reqwest::Client;
use std::time::Duration;

let client = Client::new();
let body = serde_json::json!({"model": "gpt-4o", "messages": []});
let resp = forward_request(
    &client,
    "https://api.openai.com",
    "/v1/chat/completions",
    &body,
    vec![("authorization".to_string(), "Bearer sk-...".to_string())],
    Duration::from_secs(60),
).await;