英文:
Golang request body keep giving EOF error
问题
我有以下的Go程序,我想要读取通过POST发送的JSON值。我尝试了几种不同的方法,例如首先使用r.Body = http.MaxBytesReader(w, r.Body, 1048576)
,然后再运行Json.NewDecoder(r.Body).Decode(&u)
。所有的方法似乎都会出现相同的EOF错误。我甚至尝试了body, err := ioutil.ReadAll(r.Body)
。所以所有的方法看起来都是一样的。
func insertUser(w http.ResponseWriter, r *http.Request) {
type User struct {
Uemail string `json:"uEmail"`
Upass string `json:"uPass"`
}
var u User
if r.Method == "POST" {
if r.Header.Get("Content-Type") != "" {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
if value != "application/json" {
msg := "Content-Type header is not application/json"
json.NewEncoder(w).Encode(Errormessage{msg});
return
}
}
//r.Body = http.MaxBytesReader(w, r.Body, 1048576)
/*body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}*/
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
fmt.Println("Decoding error is :", err)
json.NewEncoder(w).Encode(Errormessage{"Message decoding Issue"});
return
}
var uEmail string = u.Uemail
var uPass string = u.Upass
fmt.Println("User Namess :", uEmail)
fmt.Println("User Password :", uPass)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
}
}
英文:
I have the following Go program which I want to read the JSON values send from via post. I have tried few different methods example r.Body = http.MaxBytesReader(w, r.Body, 1048576)
first then only run Json.NewDecoder(r.Body).Decode(&u)
. All methods seems to be giving the same EOF error. I have even tried this body, err := ioutil.ReadAll(r.Body)
. So all seem the same
func insertUser(w http.ResponseWriter, r *http.Request) {
type User struct {
Uemail string `json:"uEmail"`
Upass string `json:"uPass"`
}
var u User
if r.Method == "POST" {
if r.Header.Get("Content-Type") != "" {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
if value != "application/json" {
msg := "Content-Type header is not application/json"
json.NewEncoder(w).Encode(Errormessage{msg});
return
}
}
//r.Body = http.MaxBytesReader(w, r.Body, 1048576)
/*body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}*/
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
fmt.Println("Decoding error is :", err)
json.NewEncoder(w).Encode(Errormessage{"Message decoding Issue"});
return
}
var uEmail string = u.Uemail
var uPass string = u.Upass
fmt.Println("User Namess :", uEmail)
fmt.Println("User Password :", uPass)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
}
答案1
得分: 1
请尝试运行以下代码:
func insertUser(w http.ResponseWriter, r *http.Request) {
type User struct {
Uemail string `json:"uEmail"`
Upass string `json:"uPass"`
}
var u User
if r.Method == "POST" {
if r.Header.Get("Content-Type") != "" {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
if value != "application/json" {
msg := "Content-Type header is not application/json"
json.NewEncoder(w).Encode(Errormessage{msg})
return
}
}
// CODE CHANGED HERE
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
// handle error
return
}
err = json.Unmarshal(body, &u)
if err != nil {
// handle error
return
}
// CHANGED TILL HERE
var uEmail string = u.Uemail
var uPass string = u.Upass
fmt.Println("User Namess :", uEmail)
fmt.Println("User Password :", uPass)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
}
}
这段代码是一个用于插入用户的函数。它首先定义了一个名为User
的结构体,包含了Uemail
和Upass
两个字段。然后,它检查请求的方法是否为POST,并且检查请求头中的Content-Type是否为application/json。如果不是,它会返回一个错误消息。
接下来,代码读取请求体中的数据,并将其解析为User
结构体。然后,它将Uemail
和Upass
赋值给相应的变量,并打印出来。
最后,代码设置响应头的Content-Type为application/json,并返回响应。
英文:
Please try running the following
func insertUser(w http.ResponseWriter, r *http.Request) {
type User struct {
Uemail string `json:"uEmail"`
Upass string `json:"uPass"`
}
var u User
if r.Method == "POST" {
if r.Header.Get("Content-Type") != "" {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
if value != "application/json" {
msg := "Content-Type header is not application/json"
json.NewEncoder(w).Encode(Errormessage{msg});
return
}
}
// CODE CHANGED HERE
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
// handle error
return
}
err = json.Unmarshal(body, &u)
if err != nil {
// handle error
return
}
// CHANGED TILL HERE
var uEmail string = u.Uemail
var uPass string = u.Upass
fmt.Println("User Namess :", uEmail)
fmt.Println("User Password :", uPass)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论