Skip to main content

HttpClient

Struct HttpClient 

Source
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>

Source

pub fn new(stack: &'a Stack<'a>) -> Self

Create a new HTTP client with custom buffer sizes and default options

Source

pub fn with_options(stack: &'a Stack<'a>, options: HttpClientOptions) -> Self

Create a new HTTP client with custom buffer sizes and custom options

Source

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 request
  • body - 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 read
  • Err(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(())
}
Source

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 request
  • body - The request body data
§Returns
  • Ok(HttpResponse) - Successful response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 request
  • body - The request body data
§Returns
  • Ok(HttpResponse) - Successful response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 request
  • body - The request body data
§Returns
  • Ok(HttpResponse) - Successful response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Source

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 response
  • Err(Error) - Error occurred during the request process
§Errors

Returns the same errors as HttpClient::request.

Auto Trait Implementations§

§

impl<'a, const TCP_RX: usize, const TCP_TX: usize, const TLS_READ: usize, const TLS_WRITE: usize, const RQ: usize> Freeze for HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>

§

impl<'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> !RefUnwindSafe for HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>

§

impl<'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> !Send for HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>

§

impl<'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> !Sync for 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> Unpin for HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>

§

impl<'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> !UnwindSafe for HttpClient<'a, TCP_RX, TCP_TX, TLS_READ, TLS_WRITE, RQ>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.