1 回答
TA贡献1829条经验 获得超7个赞
您必须检查来自json.Unmarshal或来自的错误decoder.Decode。
它"expires_in": 299,不是时间,它是 int。
代码:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry int `json:"expires_in"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var authToken Token
if err := decoder.Decode(&authToken); err != nil {
fmt.Fprintf(w, "error: %s", err)
return
}
fmt.Println("-------------------- accessToken " + authToken.AccessToken)
fmt.Println("-------------------- refreshToken " + authToken.RefreshToken)
fmt.Println("-------------------- expires ", authToken.Expiry)
fmt.Println("-------------------- type " + authToken.TokenType)
})
http.ListenAndServe(":8000", nil)
}
卷曲:
curl -XPOST 'http://localhost:8000' -H 'Content-Type: application/json' -d '{
"access_token": "at",
"refresh_token": "rt",
"expires_in": 299,
"token_type": "bearer"
}'
结果:
-------------------- accessToken at
-------------------- refreshToken rt
-------------------- expires 299
-------------------- type bearer
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报