我正在使用fetch API 将两个值发送到我的 POST 请求处理程序:
fetch('http://localhost:8080/validation', {
method:'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email:this.state.email,
password:this.state.password
})
我想在服务器端保存电子邮件和密码为字符串,以下是我的尝试:
type credentials struct {
Test string
}
func Validate(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
decoder := json.NewDecoder(req.Body)
var creds credentials
err := decoder.Decode(&creds)
if err != nil {
panic(err)
}
fmt.Println(creds.Test)
}
问题是我不知道发送到 POST 结构的确切格式。我正在尝试将 req.Body 保存为字符串,但是没有得到任何结果。
当输入 fmt.Println 只得到一个空格,解析它的正确方法是什么?