HttpBody Messages

The HTTPBody messages allow a response message to be specified with custom data content and a custom content-type header. The values included in the HTTPBody response will be used verbatim in the returned message from the gateway. Make sure you format your response carefully!

Example Usage

  1. Define your service in gRPC with an httpbody response message
  1. import "google/api/httpbody.proto";
  2. import "google/api/annotations.proto";
  3. import "google/protobuf/empty.proto";
  4. service HttpBodyExampleService {
  5. rpc HelloWorld(google.protobuf.Empty) returns (google.api.HttpBody) {
  6. option (google.api.http) = {
  7. get: "/helloworld"
  8. };
  9. }
  10. rpc Download(google.protobuf.Empty) returns (stream google.api.HttpBody) {
  11. option (google.api.http) = {
  12. get: "/download"
  13. };
  14. }
  15. }
  1. Generate gRPC and reverse-proxy stubs and implement your service.

Example service implementation

  1. func (*HttpBodyExampleService) Helloworld(ctx context.Context, in *empty.Empty) (*httpbody.HttpBody, error) {
  2. return &httpbody.HttpBody{
  3. ContentType: "text/html",
  4. Data: []byte("Hello World"),
  5. }, nil
  6. }
  7. func (HttpBodyExampleService) Download(_ *empty.Empty, stream HttpBodyExampleService_DownloadServer) error {
  8. msgs := []*httpbody.HttpBody{
  9. {
  10. ContentType: "text/html",
  11. Data: []byte("Hello 1"),
  12. },
  13. {
  14. ContentType: "text/html",
  15. Data: []byte("Hello 2"),
  16. },
  17. }
  18. for _, msg := range msgs {
  19. if err := stream.Send(msg); err != nil {
  20. return err
  21. }
  22. }
  23. return nil
  24. }