1

我正在使用 Postman 在 localhost 上发布 json 字符串。我在 Postman 中传递的 json 字符串是:

{
    “name”: "foo"
} 

然而,当我在我的测试函数中检索数据时,req.Body我得到了这样的结果:&{%!s(*io.LimitedReader=&{0xc0820142a0 0}) <nil> %!s(*bufio.Reader=<nil>) %!s(bool=false) %!s(bool=true) {%!s(int32=0) %!s(uint32=0)} %!s(bool=true) %!s(bool=false) %!s(bool=false)}

我希望在请求正文中获得 name:foo 。

我的 go lang 代码是:

import (
    "encoding/json"
    "fmt"
    "net/http"
)

type Input struct {
    Name string `json:"name"`
}


func test(rw http.ResponseWriter, req *http.Request) {
    var t Input
    json.NewDecoder(req.Body).Decode(&t)
    fmt.Fprintf(rw, "%s\n", req.Body)
}

func main() {
    http.HandleFunc("/test", test)
    http.ListenAndServe(":8080", nil)
}

谁能告诉我为什么我在 req.Body 属性中得到空白数据?非常感谢。

4

1 回答 1

1
于 2015-10-20T10:58:59.760 回答