Golang请求体一直报EOF错误。

huangapple go评论75阅读模式
英文:

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的结构体,包含了UemailUpass两个字段。然后,它检查请求的方法是否为POST,并且检查请求头中的Content-Type是否为application/json。如果不是,它会返回一个错误消息。

接下来,代码读取请求体中的数据,并将其解析为User结构体。然后,它将UemailUpass赋值给相应的变量,并打印出来。

最后,代码设置响应头的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")
  }
}

huangapple
  • 本文由 发表于 2022年4月13日 20:41:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71857665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定