Skip to content

Instantly share code, notes, and snippets.

@f3nry f3nry/main.go
Created Feb 1, 2019

What would you like to do?
Third Attempt: Length-prefix Construction
const (
payloadLen = 1
sizeLen = 4
headerLen = payloadLen + sizeLen
func msgHeader(data []byte) (hdr []byte, payload []byte) {
hdr = make([]byte, headerLen)
hdr[0] = byte(uint8(0))
// Write length of payload into buf
binary.BigEndian.PutUint32(hdr[payloadLen:], uint32(len(data)))
return hdr, data
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
message := &api.AliveResponse{Message: "Hello, world."}
b, err := proto.Marshal(message)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 500,
}, err
hdr, data := msgHeader(b)
hdr = append(hdr, data...)
return events.APIGatewayProxyResponse{
Body: base64.StdEncoding.EncodeToString(hdr),
Headers: map[string]string{
"Content-Type": "application/grpc+proto",
"grpc-status": "0",
IsBase64Encoded: true,
StatusCode: 200,
}, nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.