使用golang处理protobuf和处理[]byte类型的HTTP响应体。

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

Using protobuf with golang and handling []byte HTTP response body

问题

我正在使用Golang的protobuf包,并尝试编写一些测试来确保我的API正常工作。

我在服务器端使用生成的.pb.go文件构建一个对象。

然后用以下代码返回它:

data, err := proto.Marshal(p)
fmt.Fprint(w, data)

在我的测试中,我这样做:

func TestGetProduct(t *testing.T) {
    log.Println("Starting server")
    go startAPITestServer()
    time.Sleep(0 * time.Second)
    log.Println("Server started")
    //rq, err := http.NewRequest("GET", "localhost:8181/product/1", nil)
    client := &http.Client{}
    log.Println("Starting Request")
    resp, err := client.Get("http://localhost:8181/product/1")
    log.Println("Finished Request")
    if err != nil {
        t.Log(err)
    }
    defer resp.Body.Close()
    log.Println("Reading Request")
    data, err := ioutil.ReadAll(resp.Body)
    log.Println("Reading finished")
    if err != nil {
        t.Log(err)
    }
    log.Println("HTTP Resp", data)
    p := &Product{}
    proto.UnmarshalText(string(data), p)
    proto.Unmarshal(data, p2)
}

问题是,HTTP请求是正确的,并且正确显示了[]byte,但是如果我使用ioutil.ReadAll,它会将HTTP响应解释为字符串并将其转换为[]byte

例如,响应是

[12 3 2 14 41]

然后ioutil.ReadAll将其解释为字符串而不是[]byte

英文:

I am using the Golang protobuf package and try to write some tests to ensure my API works properly.

I construct an Object on the server-side with a generated .pb.go file.

And return it with

data, err := proto.Marshal(p)
fmt.Fprint(w, data)

And in my test I do

func TestGetProduct(t *testing.T) {
	log.Println("Starting server")
	go startAPITestServer()
	time.Sleep(0 * time.Second)
	log.Println("Server started")
	//rq, err := http.NewRequest("GET", "localhost:8181/product/1", nil)
	client := &http.Client{}
	log.Println("Starting Request")
	resp, err := client.Get("http://localhost:8181/product/1")
	log.Println("Finished Request")
	if err != nil {
		t.Log(err)
	}
	defer resp.Body.Close()
	log.Println("Reading Request")
	data, err := ioutil.ReadAll(resp.Body)
	log.Println("Reading finished")
	if err != nil {
		t.Log(err)
	}
	log.Println("HTTP Resp", data)
	p := &Product{}
	proto.UnmarshalText(string(data), p)
	proto.Unmarshal(data, p2)
}

The Problem is that the HTTP Request is correct and displays the []byte correctly, but if I do ioutil.ReadAll it interprets the HTTP Response as a string and converts it to a []byte.

For example the response is

[12 3 2 14 41]

Then ioutil.ReadAll interprets this as a string and not as a []byte.

答案1

得分: 5

问题是:我尝试使用fmt.Fprint将二进制数据写入输出流,但忽略了一个重要事实,即fmt包会将输入(所有内容?)转换为“可读取”的格式(即字符串)。
将数据写入HTTP响应的正确方法是直接使用responsewriter,像这样:

k, err := w.Write(data)
英文:

The problem was: I tried to write binary data to the output stream with fmt.Fprint missing the important fact, that the fmt package converts (everything?) input to a "read-able" format (ie strings).
The correct way of writting data into the output of your HTTP Response is using the responsewriter directly like this:

k, err := w.Write(data)

huangapple
  • 本文由 发表于 2015年6月14日 23:28:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/30831162.html
匿名

发表评论

匿名网友

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

确定