1

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") } 
12
  • I am just sending the request via postman I can confirm it send not empty Commented Apr 13, 2022 at 12:54
  • I dont understand in my code I dont ready before this only part I check the error ensure the content type is application/json other than that I dont touch the body in that part above. Commented Apr 13, 2022 at 12:56
  • 1
    Either there is no body, or something read it already. Are you using any other handlers or middleware? Can you provide a minimal reproducible example? What you have here (without ioutil.ReadAll(r.Body) of course) does work as expected. Commented Apr 13, 2022 at 13:10
  • Sure let me provide more codes cause I minimize the codes to avoid too messy. Commented Apr 13, 2022 at 13:35
  • @jimB I have put my full working codes here pastebin.com/UFbmJTrP. Its the complete codes I remove everything else there. Commented Apr 13, 2022 at 13:39

1 Answer 1

0

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") } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.