Skip to content

affected/package: encoding/json can not encoding Http Response entity #50307

Closed
@ghost

Description

What version of Go are you using (go version)?

$ go version
1.17.5

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/nomore/Library/Caches/go-build"
GOENV="/Users/nomore/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/nomore/go/pkg/mod"
GONOPROXY=""
GONOSUMDB="*"
GOOS="darwin"
GOPATH="/Users/nomore/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn/"
GOROOT="/Users/nomore/.asdf/installs/golang/1.17.5/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/nomore/.asdf/installs/golang/1.17.5/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/s8/w2xf8qxj6v712bq3_w_p7clw0000gp/T/go-build3423845732=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

     // Click here and start typing.
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	hc, err := http.NewRequest("GET", "https://gitlab.com/api/v4/templates/gitignores", bytes.NewBuffer([]byte{}))
	if err != nil {
		panic(err)
	}

	clt := http.Client{}
	response, err := clt.Do(hc)
	jsonStr, err := json.Marshal(response)
	fmt.Printf("Response: '%+v' \n", string(jsonStr))

	fmt.Printf("Response raw: '%+v'\n", response)

	bodyAll, err := ioutil.ReadAll(response.Body)

	fmt.Println("response status: " + response.Status)
	fmt.Println("body " + string(bodyAll))
}

What did you expect to see?

fmt.Printf("Response: '%+v' \n", string(jsonStr))
return the formatted JSON string.

What did you see instead?

fmt.Printf("Response: '%+v' \n", string(jsonStr))

Empty string

For compare, With Java , we can easy to dump all response object into a JSON String

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions