HTTP Provider
This document describes the supported HTTP Providers, their configurations, and how they can be used to send requests and process responses.
Overviewβ
The HttpProvider allows sending HTTP requests using either:
- Structured HTTP Requests β Specify the URL, HTTP method, headers, and body.
- Raw HTTP Requests β Define the full HTTP request manually as a raw string.
It also supports:
- Request Transformations (
transform_request) β Modify the request before sending. - Response Transformations (
transform_response) β Extract or process the API response.
1. Structured HTTP Provider (http-parsed)β
A structured HTTP provider defines an HTTP request using explicit fields such as url, method, headers, and body.
Configuration Fieldsβ
| Field | Type | Required | Description |
|---|---|---|---|
provider | "http" | β Yes | Must always be "http". |
config.type | "http-parsed" | β Yes | Discriminator for structured provider. |
config.url | str | β Yes | The HTTP endpoint URL. |
config.method | HttpMethod | β Yes | The HTTP method (GET, POST, PUT, DELETE, PATCH). |
config.headers | dict[str, str] | β No | HTTP headers for the request (default: {}). |
config.body | Union[str, dict] | β No | Request body as a JSON object or a form-urlencoded string. |
config.transform_request | Union[str, dict] | β No | A template or function that modifies the request before sending. |
config.transform_response | Union[str, dict] | β No | A string expression, function, or file reference to process the response. |
Example Usageβ
1.1 JSON Request Exampleβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/chat"
method: POST
headers:
Content-Type: "application/json"
body:
prompt: "{{prompt}}"
model: "gpt-4o-mini"
1.2 Form-Encoded Request Exampleβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/submit"
method: POST
headers:
Content-Type: "application/x-www-form-urlencoded"
body: "username={{user}}&password={{pass}}"
2. Raw HTTP Provider (http-raw)β
A raw HTTP provider allows defining the entire HTTP request manually, including headers and body, giving full control over the request format.
Configuration Fieldsβ
| Field | Type | Required | Description |
|---|---|---|---|
provider | "http" | β Yes | Must always be "http". |
config.type | "http-raw" | β Yes | Discriminator for raw provider. |
config.raw_request | str | β Yes | The full HTTP request in raw format. |
config.use_https | bool | β No | Whether to use HTTPS (true by default). |
config.transform_response | Union[str, dict] | β No | A string expression, function, or file reference to process the response. |
Example Usageβ
2.1 Raw HTTP Requestβ
providers:
- provider: http
config:
type: http-raw
use_https: true
raw_request: |
POST /v1/chat HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer {{api_key}}
{
"model": "llama3.1-405b-base",
"prompt": "{{prompt}}",
"max_tokens": 100
}
transform_response: "json.content"
2.2 Loading Raw Request from a Fileβ
providers:
- provider: http
config:
type: http-raw
raw_request: file://requests/chat_request.txt
transform_response: "json.result.text"
3. Request Transformation (transform_request)β
Before sending a request, request transformations allow modifying the payload dynamically.
Supported Formatsβ
| Type | Example |
|---|---|
| String Template | '{"message": "{{prompt}}"}' |
| Python Lambda Function | 'lambda prompt: {"text": prompt, "timestamp": 1710341825}' |
| External File | 'file://transforms/request_transform.py:transform_request' |
Example Usageβ
3.1 Using a String Templateβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/chat"
transform_request: '{"message": "{{prompt}}"}'
body:
user_message: "{{prompt}}"
3.2 Using a Python Functionβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/chat"
transform_request: "lambda prompt: {'text': prompt, 'timestamp': 1710341825}"
3.3 Loading a Request Transform from Fileβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/chat"
transform_request: "file://transforms/request_transform.py"
4. Response Transformation (transform_response)β
After receiving a response, response transformations allow extracting or modifying the output before returning it.
Supported Formatsβ
| Type | Example |
|---|---|
| JSON Path | 'json.choices[0].message.content' |
| Python Lambda Function | 'lambda json, text, context: json["choices"][0]["message"]["content"]' |
| External File | 'file://parsers/response_parser.py:parse_response' |
Example Usageβ
4.1 Extracting JSON Dataβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/completions"
transform_response: "json.choices[0].message.content"
4.2 Using a Lambda Functionβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/generate"
transform_response: "lambda json, text, context: json['result']['message']"
4.3 Loading Response Transform from Fileβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/parse"
transform_response: "file://parsers/response_parser.py"
5. Query Parametersβ
You can dynamically generate query parameters using placeholders.
Exampleβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/search"
method: GET
queryParams:
q: "{{prompt}}"
lang: "en"
6. Nested JSON Objects in Request Bodyβ
When working with complex request payloads, use dump to serialize nested objects.
Exampleβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/generate"
body:
messages: "{{messages | dump}}"
model: "gpt-4o-mini"
7. Using Environment Variablesβ
Environment variables allow you to avoid hardcoding secrets (like API keys or tokens).
You can reference them in your config using {{VAR_NAME}} placeholders, and then provide their values under environments.
Configuration Fieldsβ
| Field | Description |
|---|---|
{{PLACEHOLDER}} | Placeholder inside your provider config (e.g., headers, body). |
environments.vars | Defines the actual values to be substituted. |
{{env.ENV_NAME}} | Reference to system environment variables (secure injection). |
Example 7.1 β OpenAI API with Placeholderβ
providers:
- provider: http
config:
type: http-raw
use_https: true
raw_request: |
POST /v1/chat/completions HTTP/1.1
Host: api.openai.com
Content-Type: application/json
Authorization: Bearer {{OPENAI_API_KEY}}
{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are an AI security model trained to detect jailbreak and prompt injection attempts."},
{"role": "user", "content": "{{prompt}}"}
],
"temperature": 0.0,
"max_tokens": 100,
"n": 1
}
transform_response: "json.choices[0].message.content"
environments:
- vars:
OPENAI_API_KEY: "{{env.OPENAI_API_KEY}}"
β Here:
{{OPENAI_API_KEY}}is injected into the raw request.- The actual value is pulled from your system environment variable
OPENAI_API_KEY.
Example 7.2 β Multiple Environment Variablesβ
providers:
- provider: http
config:
type: http-parsed
url: "https://api.example.com/secure"
method: POST
headers:
Authorization: "Bearer {{API_TOKEN}}"
X-Org-ID: "{{ORG_ID}}"
body:
query: "{{prompt}}"
environments:
- vars:
API_TOKEN: "{{env.MY_API_TOKEN}}"
ORG_ID: "my-team-123"
β This example shows:
API_TOKENis read from system env varMY_API_TOKEN.ORG_IDis set directly in the YAML config (hardcoded placeholder).
Example 7.3 β Safe Defaultsβ
You can also provide defaults inline in case an environment variable is not set:
environments:
- vars:
TIMEOUT: "{{env.REQUEST_TIMEOUT | default(30)}}"
Here, if REQUEST_TIMEOUT isnβt set, the value falls back to 30.