hans-http

Crates.iohans-http
lib.rshans-http
version0.2.0
sourcesrc
created_at2024-10-16 13:52:14.705285
updated_at2024-10-16 14:24:40.850292
descriptionWriting a http request and http response safely.
homepage
repositoryhttps://github.com/HansHazwan/hans-http
max_upload_size
id1411831
size9,535
Hans (HansHazwan)

documentation

README

Write http request and http response safely.

Build a http request:

use hans_http::{RequestMethod, HttpRequestBuilder, Error};

fn main() -> Result<(), Error> {
    let content = "{\"username\": \"Hans\"}";
    let content_len = content.len().to_string();

    let http_request = HttpRequestBuilder::new()
        .set_request_method(RequestMethod::POST)
        .set_url("/users")
        .add_header(("Content-Type", "application/json"))
        .add_header(("Content-Length", &content_len))
        .set_body(content)
        .build()?;

    // Result
    println!("{:?}", http_request);
    println!("{}", http_request.to_string());

    Ok(())
}

Build a http response

use hans_http::{StatusCode, HttpResponseBuilder, Error};

fn main() -> Result<(), Error> {
    let content = "{\"username\": \"Hans\"}";
    let content_len = content.len().to_string();

    let http_response = HttpResponseBuilder::new()
        .set_status_code(StatusCode::Ok)
        .add_header(("Content-Type", "application/json"))
        .add_header(("Content-Length", &content_len))
        .set_body(&content)
        .build()?;

    // Result
    println!("{:?}", http_response);
    println!("{}", http_response.to_string());

    Ok(())
}

Commit count: 1

cargo fmt