所以我在代码中尝试了基于示例here的内容,并且没有数据,但也没有错误。代码是:
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"接受:application / json" --data" {\"用户名\":\" x \",\"密码\":\" y \"}" 127.0.0.1:8000/login -i
服务器打印出来:
凭证:,
为什么cred.username和cred.password中没有任何内容?
答案 0 :(得分:4)
username
更改为Username