Hi i want to read the json body of a POST request 
type MyData struct { 
grant_type string json:"grant_type" 
username  string  json:"username" 
password string	  json:"password"
} 
func main() { 
http.HandleFunc("/", requestHandler) 
log.Fatal(http.ListenAndServe(“127.0.0.1:8085”, nil)) 
} 
func requestHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var t MyData
err := decoder.Decode(&t)
if err != nil{
	panic(err)
}
fmt.Print("======================================>>>>>>>>>>>>>>>>>>",decoder) 
             
            
               
               
               
            
            
           
          
            
              
                NobbZ  
                (Norbert Melzer)
               
              
                  
                    June 4, 2018, 10:49am
                   
                   
              2 
               
             
            
              Expose the fields on the struct or JSON can’t see them.
             
            
               
               
              2 Likes 
            
            
           
          
            
            
              Expanding on @NobbZ  response, in your MyData type you need to make the fields start with capital letters like so:
type MyData struct {
  GrantType string json:"grant_type"
  Username string json:"username"
  Password string json:"password"
}
 
The encoding/json package will not fill in fields that are not exported.
             
            
               
               
              2 Likes 
            
            
           
          
            
            
              json sent via post man 
{ 
“grant_type”: “password”, 
“username”: “m”, 
“password”: “Password” 
}
             
            
               
               
               
            
            
           
          
            
              
                NobbZ  
                (Norbert Melzer)
               
              
                  
                    June 5, 2018, 12:15pm
                   
                   
              5 
               
             
            
              No, thats not what I meant.
What I meant was, that you need  to expose the fields from the struct you want to marschall into/from.
The JSON (de)serializer wont be able to see and manipulate the fields if they are not exposed.
@joncalhoun  explained pretty well how to do that .
             
            
               
               
               
            
            
           
          
            
              
                system  
                (system)
                  Closed 
               
              
                  
                    September 3, 2018, 12:17pm
                   
                   
              6 
               
             
            
              This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.