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 属性に空白のデータが表示される理由を誰か教えてもらえますか? どうもありがとう。