2 unstable releases

new 0.2.0 Oct 16, 2024
0.1.0 Oct 16, 2024

#1 in #safely

Download history 260/week @ 2024-10-14

260 downloads per month

MIT/Apache

10KB
207 lines

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(())
}

No runtime deps