无法解码 vapor swift post 请求的客户端响应
Cant decode client response on vapor swift post request
我正在使用 vapor swift 客户端发出 post 请求。我的代码看起来像这样
_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
returns 完整的 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?
}
我正在使用 vapor swift 客户端发出 post 请求。我的代码看起来像这样
_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
returns 完整的 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?
}