[英]JSON decoding in golang
所以,我想基于示例的东西在这里在我的代码,并没有得到数据,但没有任何错误。 代码是:
import (
"io"
"fmt"
"net/http"
"encoding/json"
)
type Credential struct {
username string `json:"username"`
password string `json:"password"`
}
func login(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
cred := Credential{}
err := json.NewDecoder(req.Body).Decode(&cred)
if err != nil {
panic("can't decode")
}
fmt.Println("credentials: " + cred.username + " , " + cred.password)
}
}
我测试
curl -X POST -H“ Accept:application / json” --data“ {\\” username \\“:\\” x \\“,\\” password \\“:\\” y \\“}” 127.0.0.1:8000/login -一世
服务器将输出:
凭据:
为什么cred.username和cred.password中没有任何内容?
golang使用该字段的第一个字符对该结构声明public或private。 因此将username
更改为Username
username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.