无法对蒸汽快速发布请求的客户端响应进行解码

问题描述

我正在使用 Steam Swift 客户端发出发布请求。我的代码看起来像这样

 _req.client.post("https://oauth2.googleapis.com/token") { req in
    try req.content.encode( [
        "code": code,"grant_type": "authorization_code","redirect_uri": "http://localhost:8080/googleAuth"
    ])
}.flatMapThrowing { response in
    try response.content.decode(Accesstoken.self)
}.map{ json in
    print(json)
}

flatMapThrowing 返回完整的 http 响应,但是当我尝试使用 .map 对其进行解码时,我得到一个 nil。

这是我从 flatMapThrowing 得到的回应

HTTP/1.1 200 OK
Cache-Control: no-cache,no-store,max-age=0,must-revalidate
Expires: Mon,01 Jan 1990 00:00:00 GMT
Pragma: no-cache
Date: Fri,23 Apr 2021 16:03:57 GMT
Content-Type: application/json; charset=utf-8
vary: X-Origin
vary: Referer
Server: scaffolding on HTTPServer2
X-XSS-Protection: 0
x-frame-options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
vary: Origin,Accept-Encoding
transfer-encoding: chunked

{
  "access_token": "...","expires_in": 3599,"refresh_token": "...","scope": "...","token_type": "Bearer","id_token": "..."
}

但是当我尝试用这个模型解码它时,我得到了一个空变量

import Fluent
import Vapor

struct Accesstoken: Content {
    var access_token: String
    var expires_in: String?
    var refresh_token: String?
    var scope: String?
    var token_type: String?
    var id_token: String?
}

解决方法

正如 Nick 所说,模型在 expires_in: String? 中有错误,我解码了 json,将模型更改为:

import Fluent
import Vapor

struct AccessToken: Content {
    var access_token: String?
    var expires_in: Int?
    var refresh_token: String?
    var scope: String?
    var token_type: String?
    var id_token: String?
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...