What is the correct way to send binary data using protocol buffers?

After using the protogen tool, I have a message type for sending messages:

type File struct {
    Info string `protobuf:"bytes,1,opt,name=info,json=info" json:"info,omitempty"`
    BytesValues []byte  `protobuf:"bytes,2,opt,name=bytes_values,json=bytesValues,proto3" json:"bytes_values,omitempty"`
}

I am trying to send some binary data using a field BytesValuesas follows:

filePath := filepath.Join("test", "myfile.bin")

f, _ := ioutil.ReadFile(filePath) // error return value ignored for brevity

msg := File{BytesValues: f}

body, _ := proto.Marshal(msg) // encode

The server has problems decrypting the message that I send to it. Is this the right way to send binary data using a []byteprotocol buffer field ?

+4
source share
1 answer

In my case, the problem was actually a server that was not reading raw bytes from the correct field.

- . - , .

filePath := filepath.Join("test", "myfile.bin")

f, _ := ioutil.ReadFile(filePath) // error return value ignored for brevity

msg := File{BytesValues: f}

body, _ := proto.Marshal(msg) // encode
0

All Articles