&'static str { r#" -6ren">
gpt4 book ai didi

rust - 火箭不设置内容类型文本/html

转载 作者:行者123 更新时间:2023-11-29 07:46:03 24 4
gpt4 key购买 nike

我正在使用 Rust Rocket 框架生成一个简单的网页。

当转到索引页“/”时:

#[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>
"#
}

服务器控制台告诉我

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

但是我的浏览器告诉我 Content-Type 是 text/plain

如何让 Rocket 以 text/html 正确响应。是我做错了什么还是 Rocket 做错了?

最佳答案

guide about responders解释了如何设置响应的 Content-Type。特别是,您需要 rocket::response::content::Html :

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

请注意,如果您要将 Content-Type 设置为“text/html”,您实际上必须返回一个 HTML 文档。您在示例代码中发布的内容只是 HTML 的一个片段。实际上,将 HTML 放入静态 foo.html 文件并使用 NamedFile 会容易得多。直接提供服务(自动设置 Content-Type),或 use templates .

关于rust - 火箭不设置内容类型文本/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com