
gRPC transcoding syntax is a powerful tool for building robust APIs, allowing developers to transform data between different formats.
The gRPC transcoding syntax is based on a simple and readable syntax, making it easy to understand and implement.
In this syntax, you can use the `syntax = "proto3";` directive to specify the protocol buffer syntax version.
The `syntax` directive is a required statement in the `.proto` file, and it must be the first statement in the file.
gRPC Transcoding Syntax Basics
To expose gRPC services via HTTP calls, we use JSON Transcoding. This allows us to access our gRPC services by making HTTP requests.
There are two sample proto files in the Bookstore sample, which can be used to deploy the Bookstore with or without transcoding mappings. The difference between these two files is the presence of transcoding bindings in the http_bookstore.proto file.
gRPC methods must be annotated with an HTTP rule before they support transcoding. This HTTP rule includes information about how to call the gRPC method, such as the HTTP method and route.
Here are the key elements of an HTTP rule:
- HTTP method (e.g. GET)
- Route (e.g. /v1/greeter/{name})
- Binding of request message fields to route parameters
What Is gRPC? Pros and Cons
gRPC is a high-performance RPC framework that uses HTTP/2 and Protobuf (protocol buffers).
It's a powerful tool for building scalable and efficient APIs. gRPC is designed to handle high traffic and large amounts of data. It's particularly useful for microservices architecture and real-time data processing.
One of the main advantages of gRPC is its ability to use HTTP/2, which enables efficient multiplexing of multiple requests over a single connection. This can significantly reduce latency and improve performance.
However, gRPC also has some limitations, including a steeper learning curve compared to other RPC frameworks.
Broaden your view: Grpc vs Rest Performance
Annotate gRPC Methods
A gRPC method must be annotated with an HTTP rule before it supports transcoding. This HTTP rule includes information about how to call the gRPC method, such as the HTTP method and route.
The HTTP rule is specified using the name google.api.http. This is the key to unlocking transcoding capabilities for your gRPC method.
The method is accessible with GET requests and the /v1/greeter/{name} route. The name field on the request message is bound to a route parameter.
Explore further: Grpc over Http
To customize how a gRPC method binds to a RESTful API, you can explore the options available for annotating gRPC methods and customizing JSON. This is where the magic happens, and you can create a seamless integration between your gRPC service and the outside world.
For example, you can define a Greeter service with a SayHello method, and specify the HTTP rule using the google.api.http name. This will allow you to call the gRPC method with a GET request and the /v1/greeter/{name} route.
Here's a summary of the HTTP rule syntax:
- google.api.http: specifies the HTTP rule
- GET: indicates the HTTP method
- /v1/greeter/{name}: specifies the route
- name: binds the request message field to a route parameter
Enum Extensions.Filters.Http.UrlUnescapeSpec
Enum Extensions.Filters.Http.UrlUnescapeSpec is a crucial aspect of gRPC transcoding syntax. It determines how URL path parameters are decoded.
By default, URL path parameters will not decode RFC 6570 reserved characters. This means that certain characters, like %2f, will remain encoded in the URL.
However, there are cases where URL path parameters will be fully URI-decoded. For example, when the URL is %2f%23/%20%2523, it will be unescaped to /#/%23.
Configuring Transcoding

Configuring transcoding is a straightforward process that allows you to specify how data should be translated from HTTP/JSON to gRPC. gRPC transcoding is enabled by default, so you can start using it without any configuration at all.
To configure transcoding, you can use direct annotations in your .proto file or in YAML as part of your gRPC API configuration file. The recommended approach is to use proto annotations for ease of reading and maintenance.
You can specify transcoding mappings by adding annotations to your .proto file. For example, in the Bookstore sample, an annotation tells ESP that making an HTTP GET request with the URL http://mydomain/v1/shelves/1 calls the gRPC server's GetShelf() method.
There are two sample proto files in the Bookstore sample: bookstore.proto and http_bookstore.proto. The latter has transcoding bindings added, allowing you to compare the differences in the proto files.
To add transcoding mappings, you can use the following mechanisms:
- Direct annotations in your .proto file
- YAML as part of your gRPC API configuration file
For a more comprehensive guide to specifying transcoding mappings, see Standard methods, Custom methods, and the HTTP rule reference.
JSON Transcoding

JSON Transcoding is a powerful feature that allows you to expose gRPC services via HTTP calls. This means you can access your gRPC services just like any other RESTful API.
To use JSON Transcoding, you need to add two packages, Grpc.AspNetCore and Microsoft.AspNetCore.Grpc.JsonTranscoding, to your project. You can do this by running the following commands.
Here are some key points to keep in mind when using JSON Transcoding:
- gRPC methods must be annotated with an HTTP rule before they support transcoding.
- The HTTP rule includes information about how to call the gRPC method, such as the HTTP method and route.
- You can customize how a gRPC method binds to a RESTful API by using various options, such as binding a route parameter to a field on the request message.
For example, the Greeter service with a SayHello method is annotated with an HTTP rule that makes it accessible with GET requests and the /v1/greeter/{name} route. The name field on the request message is bound to a route parameter.

Here's a summary of the HTTP rule syntax:
By using JSON Transcoding, you can create a RESTful API that exposes your gRPC services in a way that's easy to use and understand.
Transcoding Options
You can add transcoding mappings to your proto files to enable transcoding.
The Bookstore sample provides two proto files to demonstrate this: bookstore.proto and http_bookstore.proto.
bookstore.proto is used in the Endpoints Tutorials and doesn't have transcoding mappings.
http_bookstore.proto has transcoding bindings added.
For a more comprehensive guide to specifying transcoding mappings, see the links provided.
Advanced Transcoding
In the Bookstore sample, you can deploy the service with or without transcoding mappings to see the differences in the proto files.
The Bookstore sample comes with two proto files: bookstore.proto and http_bookstore.proto. The former is used in the Endpoints Tutorials and doesn't have transcoding mappings, while the latter has transcoding bindings added.
To add transcoding mappings, you can use the HTTP rule reference, which provides a comprehensive guide to specifying transcoding mappings. The Standard methods and Custom methods sections of the reference also offer valuable insights into transcoding mappings.
Here's a brief comparison of the two proto files:
- bookstore.proto: used in the Endpoints Tutorials and doesn't have transcoding mappings.
- http_bookstore.proto: transcoding bindings added.
For more information on specifying transcoding mappings, be sure to check out the HTTP rule reference.
Gateway and Filters

grpc-gateway is a technology that creates RESTful JSON APIs from gRPC services. It uses the same .proto annotations to map HTTP concepts to gRPC services.
grpc-gateway uses code generation to create a reverse-proxy server. This server translates RESTful calls into gRPC+Protobuf and sends the calls over HTTP/2 to the gRPC service.
The benefit of this approach is that the gRPC service doesn't know about the RESTful JSON APIs. Any gRPC server can use grpc-gateway.
Transcoding in ASP.NET Core offers advantages to .NET app developers, including lower complexity, better performance, and lower costs. Fewer servers result in a smaller monthly hosting bill.
- Less complex: Both gRPC services and mapped RESTful JSON API run out of one ASP.NET Core app.
- Better performance: Transcoding deserializes JSON to Protobuf messages and invokes the gRPC service directly.
- Lower cost: Fewer servers result in a smaller monthly hosting bill.
Gateway
You can use grpc-gateway to create RESTful JSON APIs from gRPC services. It uses the same .proto annotations to map HTTP concepts to gRPC services.
The benefit of grpc-gateway is that the gRPC service doesn't know about the RESTful JSON APIs. This means any gRPC server can use grpc-gateway.
A reverse-proxy server is created using code generation, which translates RESTful calls into gRPC+Protobuf and sends them over HTTP/2 to the gRPC service.
A different take: Grpc Gateway

Transcoding in ASP.NET Core offers advantages to .NET app developers, including less complexity and better performance.
Here are some benefits of transcoding in ASP.NET Core:
- Less complex: Both gRPC services and mapped RESTful JSON API run out of one ASP.NET Core app.
- Better performance: Transcoding deserializes JSON to Protobuf messages and invokes the gRPC service directly.
- Lower cost: Fewer servers result in a smaller monthly hosting bill.
Extensions.Filters.Http
The Extensions.Filters.Http section is a crucial part of the gateway and filter architecture, enabling you to filter HTTP requests and responses with ease.
It allows you to apply filters to incoming HTTP requests, giving you the power to inspect and manipulate the request data before it reaches the gateway.
You can use this section to validate the HTTP method, path, and headers, ensuring that only authorized requests are processed by the gateway.
For example, you can create a filter to reject requests with a specific HTTP method, such as PUT or DELETE.
By doing so, you can prevent unauthorized changes to your system's data.
This section also provides a way to inspect and modify the HTTP request body, allowing you to extract or manipulate data as needed.
You can use this feature to parse JSON or XML data in the request body and extract relevant information.
By leveraging the Extensions.Filters.Http section, you can create a robust and secure gateway that filters out malicious requests and only allows authorized traffic to pass through.
Take a look at this: When to Use Grpc
Featured Images: pexels.com


