pub struct HttpClient<'a, const TCP_RX: usize = MEDIUM_BUFFER_SIZE, const TCP_TX: usize = MEDIUM_BUFFER_SIZE, const TLS_READ: usize = MEDIUM_BUFFER_SIZE, const TLS_WRITE: usize = MEDIUM_BUFFER_SIZE, const RQ: usize = REQUEST_SIZE> { /* private fields */ }Expand description
HTTP Client for making HTTP requests with true zero-copy response handling
This is the main client struct for making HTTP requests. It provides methods for performing GET, POST, PUT, DELETE and other HTTP requests using a zero-copy approach where all response data is borrowed directly from user-provided buffers.
The client is designed to work with Embassy’s networking stack and requires
users to provide their own response buffers, ensuring maximum memory efficiency
and control while maintaining no_std compatibility.
§Type Parameters
TCP_RX- TCP receive buffer size (default: 4096 bytes)TCP_TX- TCP transmit buffer size (default: 4096 bytes)TLS_READ- TLS read record buffer size (default: 4096 bytes, when TLS feature is enabled)TLS_WRITE- TLS write record buffer size (default: 4096 bytes, when TLS feature is enabled)RQ- HTTP request buffer size for building requests (default: 1024 bytes)
Implementations§
Source§impl<'a, const TCP_RX: usize, const TCP_TX: usize, const TLS_READ: usize, const TLS_WRITE: usize, const RQ: usize> HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>
impl<'a, const TCP_RX: usize, const TCP_TX: usize, const TLS_READ: usize, const TLS_WRITE: usize, const RQ: usize> HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>
Sourcepub fn new(stack: &'a Stack<'a>) -> Self
pub fn new(stack: &'a Stack<'a>) -> Self
Create a new HTTP client with custom buffer sizes and default options
Sourcepub fn with_options(stack: &'a Stack<'a>, options: HttpClientOptions) -> Self
pub fn with_options(stack: &'a Stack<'a>, options: HttpClientOptions) -> Self
Create a new HTTP client with custom buffer sizes and custom options
Sourcepub async fn request<'b>(
&self,
method: HttpMethod,
endpoint: &str,
headers: &[HttpHeader<'_>],
body: Option<&[u8]>,
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn request<'b>( &self, method: HttpMethod, endpoint: &str, headers: &[HttpHeader<'_>], body: Option<&[u8]>, response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Make an HTTP request with zero-copy response handling
This is the core method for making HTTP requests using zero-copy approach.
The caller provides a buffer where the response will be stored, and the
returned HttpResponse will contain references to data within that buffer.
§Arguments
method- The HTTP method to use (GET, POST, etc.)endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the requestbody- Optional request body data (required for POST/PUT requests)response_buffer- A mutable buffer to store the response data
§Returns
Ok((HttpResponse, usize))- Response with zero-copy body and bytes readErr(Error)- Error occurred during the request process
§Errors
This function will return an error if:
- The URL is malformed or cannot be parsed
- DNS resolution fails for the hostname
- Network connection cannot be established
- The request times out
- The response cannot be parsed
- The response buffer is too small for the response data
§Examples
use nanofish::{DefaultHttpClient, HttpHeader, HttpMethod, ResponseBody};
use embassy_net::Stack;
async fn example(stack: &Stack<'_>) -> Result<(), nanofish::Error> {
let client = DefaultHttpClient::new(stack);
let mut buffer = [0u8; 8192]; // You control the buffer size!
let (response, bytes_read) = client.request(
HttpMethod::GET,
"https://example.com",
&[],
None,
&mut buffer
).await?;
// Response body now contains direct references to data in buffer
match response.body {
ResponseBody::Text(text) => println!("Text: {}", text),
ResponseBody::Binary(bytes) => println!("Binary: {} bytes", bytes.len()),
ResponseBody::Empty => println!("Empty response"),
}
Ok(())
}Sourcepub async fn patch<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
body: &[u8],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn patch<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], body: &[u8], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a PATCH request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the requestbody- The request body data
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn head<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn head<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a HEAD request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn options<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn options<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making an OPTIONS request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn trace<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn trace<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a TRACE request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn connect<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn connect<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a CONNECT request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn get<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn get<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a GET request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn post<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
body: &[u8],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn post<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], body: &[u8], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a POST request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the requestbody- The request body data
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn put<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
body: &[u8],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn put<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], body: &[u8], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a PUT request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the requestbody- The request body data
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.
Sourcepub async fn delete<'b>(
&self,
endpoint: &str,
headers: &[HttpHeader<'_>],
response_buffer: &'b mut [u8],
) -> Result<(HttpResponse<'b>, usize), Error>
pub async fn delete<'b>( &self, endpoint: &str, headers: &[HttpHeader<'_>], response_buffer: &'b mut [u8], ) -> Result<(HttpResponse<'b>, usize), Error>
Convenience method for making a DELETE request
§Arguments
endpoint- The URL to request (e.g., http://example.com/api)headers- A slice of HTTP headers to include in the request
§Returns
Ok(HttpResponse)- Successful responseErr(Error)- Error occurred during the request process
§Errors
Returns the same errors as HttpClient::request.