Hi All
Let me just post my code around here to begin with–
type Userprofile struct {
First string `bson:"first" json:"first"`
Last string `bson:"last" json:"last"`
About string `bson:"about" json:"about"`
}
func RegisterUser(w http.ResponseWriter, r *http.Request){
url := r.URL.String()
log.Println(url)
var profile Userprofile
decoder := json.NewDecoder(r.Body)
decoder.Decode(&profile)
body,err := ioutil.ReadAll(io.LimitReader(r.Body,1048576))
if err!=nil{
log.Println("Error reading input json")
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &profile); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
log.Println(profile.Last)
}
When I try Postman with a POST request like –
http://localhost:3002/api/profile/create?First=Sandy&Last=Man&About=Coder
I end up getting empty/no content in the body part and as such the profile.Last ends up being empty. So I wanted to check the url received and as seen above printed it out to obtain it properly as sent. So my question is –
Why is it that I am not able to parse the params of POST with Unmarshal or NewDecoder? Is it that I need to handle it by parsing url instead of r.Body
?
NOTE–
I tried r.Formvalue() data:=r.Form.Get("Last")
This didn’t yield me any success either.
Thanks in advance.