【问题标题】:Vapor - How do I return a JSON response with a specific Status Code?Vapor - 如何返回带有特定状态代码的 JSON 响应?
【发布时间】:2016-12-15 15:26:46
【问题描述】:

免责声明:这个问题最初是在 Slack 的 Vapor 帮助频道上提出的

简单的问题:如果我要发送这样的回复

let response: ResponseRepresentable = try JSON(node: ["message": "User Created"])`

如何设置响应的状态码?

【问题讨论】:

    标签: json api httpresponse vapor


    【解决方案1】:

    你可以直接使用Response类,像这样:

    let statusCode = Status.other(statusCode: 666, reasonPhrase: "damn it")
    let response = Response(status: statusCode, json: JSON(["error": "my error"]))
    

    请参阅Response Documentation 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2021-10-11
      • 2019-08-25
      • 2012-07-07
      相关资源
      最近更新 更多