ic-http-types

Crates.ioic-http-types
lib.rsic-http-types
version0.1.0
created_at2025-04-17 11:33:36.134912+00
updated_at2025-04-17 11:33:36.134912+00
descriptionHTTP request and response types for the Internet Computer
homepage
repositoryhttps://github.com/dfinity/ic
max_upload_size
id1637536
size40,516
(gregorydemay)

documentation

https://docs.rs/ic-http-types

README

ic-http-types

ic-http-types is a Rust crate that provides types for representing HTTP requests and responses. These types are designed to simplify working with HTTP communication in canister development on the Internet Computer.

Features

  • HttpRequest: A struct for encapsulating HTTP request details, including method, URL, headers, and body.
  • HttpResponse: A struct for encapsulating HTTP response details, including status code, headers, and body.
  • HttpResponseBuilder: A builder pattern for constructing HttpResponse objects.

Usage

Add the crate to your Cargo.toml:

[dependencies]
ic-http-types = "0.1.0"

Example

use ic_canisters_http_types::{HttpRequest, HttpResponseBuilder};
use serde_bytes::ByteBuf;

fn main() {
    // Create an HTTP request
    let request = HttpRequest {
        method: "GET".to_string(),
        url: "/path/to/resource?query=1".to_string(),
        headers: vec![("Content-Type".to_string(), "application/json".to_string())],
        body: ByteBuf::defaBult(),
    };

    // Extract the path from the request URL
    println!("Path: {}", request.path());

    // Build an HTTP response
    let response = HttpResponseBuilder::ok()
        .header("Content-Type", "application/json")
        .body("{\"message\": \"success\"}")
        .build();

    println!("Response Status: {}", response.status_code);
}

Documentation

For detailed documentation, visit the Rust Docs.

License

This project is licensed under the Apache License 2.0.

Contributing

If you decide to contribute, we encourage you to announce it on the Forum!

Commit count: 33125

cargo fmt