Request with postman is ok but in case of ajax call http: panic serving XXX.XXX.XXX.XXX:XXXXX: EOF
func BodyToJson(r *http.Request) map[string]interface{}{
decoder := json.NewDecoder(r.Body);
fmt.Println(reflect.TypeOf(r.Body).Kind())
fmt.Println(decoder);
var dat map[string]interface{}
err := decoder.Decode(&dat)
if err!= nil{
panic(err);
}
return dat
}
Your application may be posting the data as Form.
You may have to use the following code to parse the data.
r.ParseForm()
defer r.Body.Close()
log.Println(r.Form)
You should check the header Content-Type. And maybe this example can be helpful.
if request.Method == "POST" {
dat := make(map[string]interface{})
if postData, err := ioutil.ReadAll(request.Body); err != nil {
panic(err)
} else if err = json.Unmarshal(postData, &dat); err != nil{
panic(err)
} else {
//use dat....
}
}
就这么爬stackoverflow的数据?