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 關注
- 144 瀏覽
添加回答
舉報