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!
google.api.HttpBody is also the native gRPC-Gateway response shape to use for binary downloads and other raw payload responses where you need to control the returned content type directly.
Example Usage
- Define your service in gRPC with an
HttpBodyresponse message.
import "google/api/httpbody.proto";
import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
service HttpBodyExampleService {
rpc HelloWorld(google.protobuf.Empty) returns (google.api.HttpBody) {
option (google.api.http) = {
get: "/helloworld"
};
}
rpc Download(google.protobuf.Empty) returns (stream google.api.HttpBody) {
option (google.api.http) = {
get: "/download"
};
}
}
- Generate gRPC and reverse-proxy stubs and implement your service.
Example service implementation
func (*HttpBodyExampleService) Helloworld(ctx context.Context, in *empty.Empty) (*httpbody.HttpBody, error) {
return &httpbody.HttpBody{
ContentType: "text/html",
Data: []byte("Hello World"),
}, nil
}
func (HttpBodyExampleService) Download(_ *empty.Empty, stream HttpBodyExampleService_DownloadServer) error {
msgs := []*httpbody.HttpBody{
{
ContentType: "text/html",
Data: []byte("Hello 1"),
},
{
ContentType: "text/html",
Data: []byte("Hello 2"),
},
}
for _, msg := range msgs {
if err := stream.Send(msg); err != nil {
return err
}
}
return nil
}