...

Text file src/github.com/grpc-ecosystem/grpc-gateway/docs/_docs/httpbody.md

Documentation: github.com/grpc-ecosystem/grpc-gateway/docs/_docs

     1---
     2category: documentation
     3---
     4
     5# HttpBody messages
     6The [HTTP Body](https://github.com/googleapis/googleapis/blob/master/google/api/httpbody.proto) messages allows 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!
     7
     8## Example Usage
     91. Create a mux and configure it to use the `HTTPBodyMarshaler`. 
    10
    11```protobuf
    12	mux := runtime.NewServeMux()
    13	runtime.SetHTTPBodyMarshaler(mux)
    14```
    152. Define your service in gRPC with an httpbody response message
    16
    17```protobuf
    18import "google/api/httpbody.proto";
    19import "google/api/annotations.proto";
    20import "google/protobuf/empty.proto";
    21
    22service HttpBodyExampleService {
    23	rpc HelloWorld(google.protobuf.Empty) returns (google.api.HttpBody) {
    24		option (google.api.http) = {
    25			get: "/helloworld"
    26		};
    27	}
    28}
    29```
    303. Generate gRPC and reverse-proxy stubs and implement your service.
    31
    32## Example service implementation
    33
    34```go
    35func (*HttpBodyExampleService) Helloworld(ctx context.Context, in *empty.Empty) (*httpbody.HttpBody, error) {
    36	return &httpbody.HttpBody{
    37		ContentType: "text/html",
    38		Data:        []byte("Hello World"),
    39	}, nil
    40}
    41```

View as plain text