Hello World with Tiny HTTP

tiny_http web Server http incoming_requests Response HeaderField Header AsciiString

See the whole series about tiny_http

Hello World

We need created a Crate using the cargo new hello-world command that will create a folder called hello-world.

Then change the directory to the newly created folder: cd hello-world and then add the tiny_http crate by running:

cargo add tiny_http

This will update the Cargo.toml file to something like this:

examples/tiny-http/hello-world/Cargo.toml

[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tiny_http = "0.12"


examples/tiny-http/hello-world/src/main.rs

use tiny_http::{Response, Server};

fn main() {
    let host = "127.0.0.1";
    let port = "5000";

    let server_str = format!("{}:{}", host, port);

    let server = Server::http(&server_str).expect("Failed to start demo server.");

    for request in server.incoming_requests() {
        request
            .respond(Response::from_string("Hello World!"))
            .unwrap();
    }
}


  • First we need to create a "server" by calling Server::http. I've added a call to expect at the end that will panic! if we operation is not successful. For example if the port is already in use.
  • Then we go into an infinite look waiting for clients (browsers) to access the server: for request in server.incoming_requests() {.
  • Then we attach our response text to the request object.

We can run the application by executing the cargo run command and then we can visit http://localhost:5000/ with our browser. It will show the text "Hello World!".

If you have curl installed you can also check the response with it:

$ curl http://127.0.0.1:5000/

Hello World

We can also use the -i flag of curl to display the head that the server sent.

$ curl -i http://127.0.0.1:5000/

HTTP/1.1 200 OK
Server: tiny-http (Rust)
Date: Mon, 11 Dec 2023 15:39:47 GMT
Content-Type: text/plain; charset=UTF-8
Content-Length: 11

Hello World

You might notice it identifies itself as tiny-http (Rust) and that the Content-Type is set to text/plain.

Hello World as HTML

Stop the server by pressing Crtl-C, then change the response text from

"Hello World!"

to

"<h1>Hello World</h1>"

restart the server by running cargo run again and reload the page in your browser. Instead of showing the text

Hello World!

in large letters it will also show the HTML tags:

<h1>Hello World</h1>

We'll have to change the Content-type if we want to tell the browser to display the content as HTML.

For this I had to first add another crate called ascii

examples/tiny-http/hello-world-as-html/Cargo.toml

[package]
name = "hello-world-as-html"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ascii = "1.1"
tiny_http = "0.12"


examples/tiny-http/hello-world-as-html/src/main.rs

use ascii::AsciiString;
use std::str::FromStr;
use tiny_http::{Header, HeaderField, Response, Server};

fn main() {
    let host = "127.0.0.1";
    let port = "5000";

    let server_str = format!("{}:{}", host, port);

    let server = Server::http(&server_str).expect("Failed to start demo server.");

    for request in server.incoming_requests() {
        let header = Header {
            field: HeaderField::from_str("Content-type").unwrap(),
            value: AsciiString::from_ascii("text/html").unwrap(),
        };
        request
            .respond(Response::from_string("<h1>Hello World</h1>").with_header(header))
            .unwrap();
    }
}


We had to add a few use-statements, create a header:

let header = Header {
    field: HeaderField::from_str("Content-type").unwrap(),
    value: AsciiString::from_ascii("text/html").unwrap(),
};

and then add it to the response by calling with_header(header).

If we now restart the server (stop by pressing Ctrl-C and then run cargo run) and then we visit the web site we'll see "Hello World!", but this time in large letters as required by the h1 HTML elements.

Running curl -i reveals that the Content-type in the header was changed to text/html that told the browser to interpret the HTML tags.

$ curl -i http://127.0.0.1:5000/

HTTP/1.1 200 OK
Server: tiny-http (Rust)
Date: Mon, 11 Dec 2023 16:12:40 GMT
Content-Type: text/html
Content-Length: 20

<h1>Hello World</h1>

Related Pages

Tiny HTTP: generate dynamic response, show the current time ⏰
Tiny HTTP: Echo using GET request
Getting started with Tiny HTTP building a web application in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo