2014-06-08 23:20
浏览 399


I'm using the Beego framework to build a web application, and I'm trying to hand it some JSON encoded data. Roughly, this is what I have:

import (

type LoginController struct {

func (this *LoginController) Post() {
  request := this.Ctx.Request
  length := request.ContentLength
  p := make([]byte, length)
  bytesRead, err := this.Ctx.Request.Body.Read(p)
  if err == nil{
  } else {
    //tell me the length, bytes read, and error

Per this tutorial, the above Should Just Work (tm).

My problem is this: bytesRead, err := this.Ctx.Request.Body.Read(p) is returning 0 bytes read and the err.Error() is EOF.

The request.ContentLength, however, is a sane number of bytes (19 or more, depending on what data I type in).

I can't figure out why the request would appear to have some length, but would fail on Read. Any ideas?

图片转代码服务由CSDN问答提供 功能建议

我正在使用Beego框架来构建Web应用程序,而我正尝试将其以JSON编码的方式进行处理 数据。 大致来说,这就是我所拥有的:

“ github.com/astaxie/beego” 
type LoginController struct {
beego.Controller \  n} 
func(this * LoginController)Post(){
 request:= this.Ctx.Request 
 length:= request.ContentLength 
p:= make([] byte,length)
 bytesRead,err  := this.Ctx.Request.Body.Read(p)
 if err == nil {
 // blah 
} else {
} \  n} 


我的问题是: bytesRead,err:= this.Ctx .Request.Body.Read(p)返回读取的0个字节,而 err.Error() EOF

request.ContentLength 是相当数量的字节(19或更多,取决于我输入的数据)。

我可以 不能弄清楚为什么请求看起来有些长,但是 ld在 Read 上失败。 有什么想法吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchun5969 2014-11-29 15:21

    If you are trying to reach a JSON payload in Beego, you'll want to call


    That returns a []byte array of the sent payload. You can then pass it to a function like:

    var datapoint Datapoint
    json.Unmarshal(this.Ctx.Input.RequestBody, &datapoint)

    Where datapoint is the struct you are attempting to unmarshall your data into.

    打赏 评论

相关推荐 更多相似问题