Skip to content

Document hostcalls::dispatch_http_call #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,47 @@ extern "C" {
) -> Status;
}

/// Initiates an HTTP call to an upstream server.
///
/// This function sends an HTTP request to the specified `upstream` server with the provided
/// `headers`, `body`, and `trailers`. The `timeout` parameter specifies the duration after
/// which the call will time out if no response is received.
///
/// # Arguments
///
/// * `upstream`: A string specifying the name of the upstream cluster to send the HTTP request to.
/// * `headers`: A vector of tuples representing the HTTP headers to include in the request. Each tuple contains a header name and its corresponding value.
/// * `body`: An optional byte slice representing the body of the HTTP request.
/// * `trailers`: A vector of tuples representing the trailers to include in the request. Each tuple contains a trailer name and its corresponding value.
/// * `timeout`: A `Duration` specifying the timeout duration for the HTTP request.
///
/// # Returns
///
/// A `Result` indicating the success or failure of the HTTP call. If the call is successful, the function returns an HTTP status code as a `u32`. If an error occurs, an `Error` object is returned.
///
/// # Example
///
/// ```rust
/// use proxy_wasm::hostcalls::dispatch_http_call;
/// use std::time::Duration;
///
/// fn main() {
/// let upstream = "my_upstream";
/// let headers = vec![("Content-Type", "application/json")];
/// let body = Some(b"request body");
/// let trailers = vec![];
/// let timeout = Duration::from_secs(5);
///
/// match dispatch_http_call(upstream, headers, body, trailers, timeout) {
/// Ok(return_token) => {
/// println!("HTTP call dispatched successfully. Return token: {}", return_token);
/// }
/// Err(status) => {
/// println!("Failed to dispatch HTTP call. Error status: {:?}", status);
/// }
/// }
/// }
/// ```
pub fn dispatch_http_call(
upstream: &str,
headers: Vec<(&str, &str)>,
Expand Down