rust - 如何在 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
上面做到这一点?
谢谢。
解决方案
与第一个示例相同,只需将 .content_type() 替换为 .set()
推荐阅读
- swift - Swift:如何解压缩我的包中不包含的文件?
- dns - 入口配置
- docker - 在 Ubuntu18.04 上使用 Fabric1.4 以“致命错误:运行时执行期间的意外信号”开始后立即退出对等体
- amazon-web-services - 使用 aws alb 入口控制器在 kubernetes pod 中公开服务
- google-apps-script - Doc Open 上的简单弹出窗口
- docker - Make: 对 docker 镜像的依赖
- c# - 收到此错误 **System.NullReferenceException: '对象引用未设置为对象的实例。'**
- python - 找出哪个图像具有最多的黑色
- jwt - Keycloak - 通过 OIDC 端点检索 JWT 令牌
- json - 如何使用烧瓶的 POST 请求传递西里尔符号