summaryrefslogtreecommitdiff
path: root/README.md
blob: 4ee8997b1362e0c00a60a0286c4ab5a1534affe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# http

A rust http parsing library

### Example

```rs
use http::{request::Request, parse::TryParse, error::HTTPError, response::Response, status, header::HeaderName};

fn main() -> Result<(), HTTPError> {

    // http request as a string
    let request_str =   "GET / HTTP/1.1\n\
                         Host: www.example.com\n\
                         User-Agent: Mozilla/4.0\n\
                         Accept-language: en\n\
                         \n\
                        ";

                            // here we set what we want the body to be parsed into if it exists
                            // the body type must implement FromStr and ToString
    let request = Request::<String>::try_parse(request_str)?; // try parse since it can fail

    let mut response = Response::<String>::new(); // create a new response
    response
            .set_body("hello world!") // add a body that is Into<T>
            .set_status(status::SUCCESS) // set the status
            .add_header((HeaderName::try_from("Content-Type")?, "text/plain")); // set content-type header

    let response_str = response.to_string(); // get http response in text form

    Ok(())
}
```

### License

This project is licensed under the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html)