Rocket does not set text of type content / html

I am using Rust Rocket to create a simple web page.

When go to the index page "/":

#[get("/")]
fn page_index() -> &'static str {
    r#"
        <title>GCD Calculator</title>
        <form action="/gcd" method="post">
            <input type="text" name="n" />
            <input type="text" name="n" />
            <button type="submit">Compute GCD</button>
        </form>
    "#
}

Server console informs me

GET / text/html:
=> Matched: GET /
=> Outcome: Success
=> Response succeeded.

But my browser tells me that Content-Type is text / plain .

How to get Rocket to respond correctly using text / html . Am I doing something wrong or is Rocket doing?

+4
source share
2 answers

The Responder Guide explains how to set the Content-Type of your answer. In particular, you need to rocket::response::content::Html:

use rocket::response::content::Html;
#[get("/")]
fn page_index() -> Html<&'static str> {
    Html(r"<html>...</html>")
}

, HTML-, Content-Type "text/html". , , - HTML. HTML foo.html NamedFile, ( Content-Type) .

+5

Rocket /, , .

Html responder :

use rocket::response::content;

#[get("/")]
fn page_index() -> content::Html<&'static str> {
    content::Html(r#"
        <title>GCD Calculator</title>
        <form action="/gcd" method="post">
            <input type="text" name="n" />
            <input type="text" name="n" />
            <button type="submit">Compute GCD</button>
        </form>
    "#)
}
+2

Source: https://habr.com/ru/post/1693887/


All Articles