Golang net/http 请求的 Body 总是为空。

huangapple go评论78阅读模式
英文:

Golang net/http request Body always empty

问题

我正在尝试将JSON参数发送到我的服务器,并使用json.Decoder解析它们。我读到可以从request.Body属性中获取查询参数。以下是我的服务器代码:

func stepHandler(res http.ResponseWriter, req *http.Request) {
    var v interface{}
    err := json.NewDecoder(req.Body).Decode(&v)
    if err != nil {
       // 处理错误
    }
    log.Println(v)
}

每次,我都看到2014/12/26 22:49:23 <nil>(当然是不同的时间戳)。我的客户端AJAX调用如下:

$.ajax({
  url: "/step",
  method: "get",
  data: {
    steps: $("#step-size").val(),
    direction: $("#step-forward").prop("checked") ? 1 : -1,
    cells: JSON.stringify(painted)
  },
  success: function (data) {
    painted = data;
    redraw();
  },
  error: function (xhr) {
    console.log(xhr);
  }
});

发送的示例URL:

http://localhost:5000/?steps=1&direction=1&cells=%5B%7B%22row%22%3A11%2C%22column%22%3A15%7D%2C%7B%22row%22%3A12%2C%22column%22%3A15%7D%5D

参数的更好展示:

{
  steps: "1",
  direction: "1",
  cells: "[{"row":11,"column":15},{"row":12,"column":15}]"
}

我尝试过使用GET和POST请求,为什么我的req.Body无法解码?如果我尝试单独打印req.Body,我也看到nil。

英文:

I'm trying to send JSON arguments to my server and parse them using json.Decoder. I've read that you should be able to get the query params from the request.Body property. The following is my server code:

func stepHandler(res http.ResponseWriter, req *http.Request) {
    var v interface{}
    err := json.NewDecoder(req.Body).Decode(&amp;v)
    if err != nil {
       // handle error
    }
    log.Println(v)
}

Every time, I see 2014/12/26 22:49:23 &lt;nil&gt; (diff timestamps, of course). My client-side AJAX call is the following:

$.ajax({
  url: &quot;/step&quot;,
  method: &quot;get&quot;,
  data: {
    steps: $(&quot;#step-size&quot;).val(),
    direction: $(&quot;#step-forward&quot;).prop(&quot;checked&quot;) ? 1 : -1,
    cells: JSON.stringify(painted)
  },
  success: function (data) {
    painted = data;
    redraw();
  },
  error: function (xhr) {
    console.log(xhr);
  }
});

An example URL of what is sent:

http://localhost:5000/?steps=1&amp;direction=1&amp;cells=%5B%7B%22row%22%3A11%2C%22column%22%3A15%7D%2C%7B%22row%22%3A12%2C%22column%22%3A15%7D%5D

A nicer look at the params:

{
  steps: &quot;1&quot;,
  direction: &quot;1&quot;,
  cells: &quot;[{&quot;row&quot;:11,&quot;column&quot;:15},{&quot;row&quot;:12,&quot;column&quot;:15}]&quot;
}

I have tried with both GET and POST requests.

Why does my req.Body never decode? If I try to print req.Body alone, I also see nil.

答案1

得分: 4

req.Body 确实是空的 -- 所以,我会调用 req.ParseForm(),然后使用 req.FormBody 不会获取请求体中明显不存在的内容(比如查询参数)。

英文:

req.Body is indeed empty -- so, what I would do it call req.ParseForm() and then use req.Form instead. Body will not get stuff (such as, query parameters) that's definitely not in the request's body.

答案2

得分: 1

请求的Body是在有效载荷中发送的,而不是URL的一部分。

你试图访问Body,但实际上你的数据在URL中。

你想要将ajax的method: "get"更改为method: "post",这样数据就会随着Body一起发送,而不是作为URL的一部分。你还应该确保数据确实通过你选择的浏览器的开发者工具发送。或者,如果你确实希望将数据作为URL的一部分发送,你应该访问请求的URL参数,并手动将值解析为结构体(json包不会为你执行此操作,如果我没记错的话)。

英文:

The Body of a request is sent along inside the payload - it is not part of the URL.

You're attempting to access the body .. when really your data is in the URL.

What you want it to change your ajax method: &quot;get&quot; to be method: &quot;post&quot; - so that the data is posted along with the body and not as part of the URL. You should also make sure that the data is indeed being sent along with the request via your browser of choice' developer tools. Alternatively, if you really do want your data sent along as part of the URL, you should be accessing the URL parameter of the request - and manually parsing the values into a struct (the json package won't do this for you IIRC).

huangapple
  • 本文由 发表于 2014年12月27日 11:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/27664192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定