Skip to content

Repository files navigation

gRPC Server

Run the server:

$ make run

It listens on port 50051, so you can grpcurl it:

$ go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
$ grpcurl -plaintext -d '{"name":"Hi"}' localhost:50051 hello.Simple/SayHello
{
  "message": "hello Hi"
}

With cURL that would be

$ printf '\x00\x00\x00\x00\x04\x0a\x02\x48\x69' | curl -sS -H "Content-Type: application/grpc" http://localhost:50051/hello.Simple/SayHello --http2-prior-knowledge -H "TE: trailers" --data-binary @- | od -v -tx1 -A n
 00 00 00 00 0a 0a 08 68 65 6c 6c 6f 20 48 69

Web Protocol

The request body is a message frame consisting of a padding/compression flag plus a length (5 bytes) then a JSON message. We construct some JSON prefixed with "00 00 00 00 0d" (uncompressed, 13 bytes) and POST it into the endpoint:

$ printf '\x00\x00\x00\x00\x0d%s' '{"name":"Hi"}' | curl -sS "Content-Type: application/grpc-web+json" 'http://localhost:50051/hello.Simple/SayHello' --data-binary @- | od -v -tx1 -A n
 00 00 00 00 16 7b 22 6d 65 73 73 61 67 65 22 3a
 22 68 65 6c 6c 6f 20 48 69 22 7d 80 00 00 00 10
 67 72 70 63 2d 73 74 61 74 75 73 3a 20 30 0d 0a

There are 2 message frames in the output. The first is the RPC response starting "00 00 00 00 16" - padding/compression plus length (20). The message is JSON text. The second message frame starts "80 00 00 00 10" so it has length 16. It's content is "grpc-status: 0" plus a new line (i.e. not JSON). The regular gRPC spec would not use 80" as a message prefix (would be "00" or "01" just like the other examples here). The gRPC Web spec mandates "80" or "81" (compressed) for the trailing header block.

You can also add an "accept" header and it's polite to use "x-user-agent: grpc-web-javascript/0.1" (not the regular HTTP user agent header), but neither is mandatory.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages