简体   繁体   English

HTTP发布请求解码问题golang EOF

[英]Http post request decoding issue golang EOF

Request with postman is ok but in case of ajax call http: panic serving XXX.XXX.XXX.XXX:XXXXX: EOF 与邮递员的请求是可以的,但如果是ajax,请致电http:紧急服务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. 您应该检查标题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....
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM