Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Forked from louiscryan/README
Created December 15, 2015 21:20
Show Gist options
  • Save emaxerrno/34a310907c4a8a90b167 to your computer and use it in GitHub Desktop.
Save emaxerrno/34a310907c4a8a90b167 to your computer and use it in GitHub Desktop.
GRPC - Curl, HTTP/1.1 & nghttp misc
# Build the GRPC Java server which is used for interop testing.
# Interface defined here
# https://github.com/grpc/grpc-java/blob/master/interop-testing/src/main/proto/io/grpc/testing/integration/test.proto
./gradlew :grpc-interop-testing:installDist
# Run it with TLS turned on so ALPN is enabled. Uses a dummy cert
./interop-testing/build/install/grpc-interop-testing/bin/test-server --port=8080 --use_tls=true
# Run the awesome nghttp2 as a proxy in front of it.
# See https://nghttp2.org/documentation/package_README.html#building-from-git
# Run in client mode, i.e HTTP 1 or 2 from the client, HTTP2 to our server.
# Uses --insecure as the GRPC server is using a dummy cert.
src/nghttpx --log-level=INFO --client --insecure -b localhost,8080 -f localhost,8081
# Ok lets create a suitable payload to use with curl. We're going to call the EmptyCall method which takes a
# 0-length protobuf so we just need to wrap that in the GRPC framing,
# from the spec (http://www.grpc.io/docs/guides/wire.html) thats...
# Delimited-Message → Compressed-Flag Message-Length Message
# Compressed-Flag → 0 / 1 # encoded as 1 byte unsigned integer
# Message-Length → {length of Message} # encoded as 4 byte unsigned integer
# Message → *{binary octet}
# which with no compression enabled this is simply
echo -n '0000000000' | xxd -r -p - frame.bin
# Now lets call our server using curl & HTTP.
# You shoud see the Grpc-Status trailer in the output with a value of 0 for "OK".
curl -v --raw -X POST -H "Content-Type: application/grpc" -H "TE: trailers" --data-binary @frame.bin http://localhost:8081/grpc.testing.TestService/EmptyCall
# We can also call the GRPC server directly using nghttp client which uses h2 directly.
# the log output will show the h2 frames.
src/nghttp -v -H ":method: POST" -H "Content-Type: application/grpc" -H "TE: trailers" --data=frame.bin https://localhost:8080/grpc.testing.TestService/EmptyCall
# If you have a version of curl with http2 support compiled in then you can also use that.
# Currently you won't see trailers (open issue)
h2curl --http2 --insecure -v -X POST -H "Content-Type: application/grpc" -H "TE: trailers" --data-binary @frame.bin https://localhost:8080/grpc.testing.TestService/EmptyCall
# ... and if you like streaming here's a sample that will generate 10 streamed response messages from the server at an interval of 1s.
echo -n '00000000501206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d1206080110c0843d' | xxd -r -p - streamingframe.bin
src/nghttp -v -H ":method: POST" -H "Content-Type: application/grpc" -H "TE: trailers" --data=streamingframe.bin https://localhost:8080/grpc.testing.TestService/StreamingOutputCall
curl -v --raw -X POST -H "Content-Type: application/grpc" -H "TE: trailers" --data-binary @streamingframe.bin http://localhost:8081/grpc.testing.TestService/StreamingOutputCall
h2curl --http2 --insecure -v -X POST -H "Content-Type: application/grpc" -H "TE: trailers" --data-binary @streamingframe.bin http://localhost:8081/grpc.testing.TestService/StreamingOutputCall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment