首页 > 解决方案 > 如何在 actix 中创建应用程序/json HTTPResponse?

问题描述

example 部分中,他们使用:

HttpResponse::Ok()
    .content_type("text/plain")
    .body(format!("Hello {}!", req.match_info().get("name").unwrap()))

但是我不喜欢硬编码,content-type因为有一个很好的表明我可以使用枚举作为json响应:

let mut builder = HttpResponse::Ok();
builder.set(ContentType(TEXT_HTML));

问题是我也想设置响应的正文。我怎样才能在builder上面做到这一点?

谢谢。

标签: rustrust-actix

解决方案


与第一个示例相同,只需将 .content_type() 替换为 .set()


推荐阅读