Your body is what you think it is, it is just printed in an unexpected format to you.
bytes.NewReader()
returns a pointer to the struct type bytes.Reader
. When you print it using log.Printf()
, it uses the formatting rules defined in the package doc of fmt
, that is, pointers to struct are printed as &{}
and values of fields enumerated inside of it. This is what you see. bytes.Reader
stores the data it provides in a byte slice:
[123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125]
And this byte slice is equivalent to the JSON string you expect. To verify:
data := []byte{123, 34, 114, 102, 114, 73, 68, 34, 58, 34, 49, 49, 49, 49, 49, 49, 34, 125}
fmt.Println(string(data))
Output:
{"rfrID":"111111"}
Reading from this reader will provide this exact string, as you can see in this example:
r := bytes.NewReader(data)
fmt.Println(r)
readData, err := ioutil.ReadAll(r)
if err != nil {
panic(r)
}
fmt.Println(string(readData))
Output:
&{[123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125] 0 -1}
{"rfrID":"111111"}
Try these examples on the Go Playground.
Differences
Let's examine the differences between the CURL command and the client you wrote, with this little app which will be the target:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
log.Println("Method:", r.Method)
log.Println("Headers:", r.Header)
log.Println("Form:", r.Form)
data, err := ioutil.ReadAll(r.Body)
log.Println("Body:", data, err)
log.Println("Body string:", string(data))
})
panic(http.ListenAndServe(":8080", nil))
CURL:
2017/10/10 10:17:26 Method: GET
2017/10/10 10:17:26 Headers: map[Content-Length:[19] User-Agent:[curl/7.47.0] Accept:[*/*] Content-Type:[application/json] Web2py-User-Token:[token-string]]
2017/10/10 10:17:26 Form: map[]
2017/10/10 10:17:26 Body: [123 34 114 102 114 73 68 34 58 32 34 49 49 49 49 49 49 34 125] <nil>
2017/10/10 10:17:26 Body string: {"rfrID": "111111"}
Go client:
2017/10/10 10:17:20 Method: GET
2017/10/10 10:17:20 Headers: map[User-Agent:[Go-http-client/1.1] Content-Length:[18] Content-Type:[application/json] Web2py-User-Token:[token-string] Accept-Encoding:[gzip]]
2017/10/10 10:17:20 Form: map[]
2017/10/10 10:17:20 Body: [123 34 114 102 114 73 68 34 58 34 49 49 49 49 49 49 34 125] <nil>
2017/10/10 10:17:20 Body string: {"rfrID":"111111"}
Despite some headers added automatically by the clients, and a space inside the JSON string (which has no effect just formatting), they are identical.