AspNetCore.Docs/aspnetcore/grpc/json-transcoding-binding.md

6.2 KiB

title author description monikerRange ms.author ms.date uid
Configure HTTP and JSON for gRPC JSON transcoding ASP.NET Core apps jamesnk Learn how to configure HTTP and JSON for gRPC JSON transcoding apps. >= aspnetcore-7.0 wpickett 09/20/2022 grpc/json-transcoding-binding

Configure HTTP and JSON for gRPC JSON transcoding

[!INCLUDE]

By James Newton-King

gRPC JSON transcoding creates RESTful JSON web APIs from gRPC methods. It uses annotations and options for customizing how a RESTful API maps to the gRPC methods.

HTTP rules

gRPC methods must be annotated with an HTTP rule before they support transcoding. The HTTP rule includes information about calling the gRPC method as a RESTful API, such as the HTTP method and route.

[!code-protobuf]

An HTTP rule is:

[!INCLUDE]

HTTP method

The HTTP method is specified by setting the route to the matching HTTP method field name:

  • get
  • put
  • post
  • delete
  • patch

The custom field allows for other HTTP methods.

In the following example, the CreateAddress method is mapped to POST with the specified route:

[!code-protobuf]

Route

gRPC JSON transcoding routes support route parameters. For example, {name} in a route binds to the name field on the request message.

To bind a field on a nested message, specify the path to the field. In the following example, {params.org} binds to the org field on the IssueParams message:

[!code-protobuf]

Transcoding routes and ASP.NET Core routes have a similar syntax and feature set. However, some ASP.NET Core routing features aren't supported by transcoding. These include:

Request body

Transcoding deserializes the request body JSON to the request message. The body field specifies how the HTTP request body maps to the request message. The value is either the name of the request field whose value is mapped to the HTTP request body or * for mapping all request fields.

In the following example, the HTTP request body is deserialized to the address field:

[!code-protobuf]

Query parameters

Any fields in the request message that aren't bound by route parameters or the request body can be set using HTTP query parameters.

[!code-protobuf]

In the preceding example:

  • org and repo fields are bound from route parameters.
  • Other fields, such as text and the nested fields from page, can be bound from the query string: ?text=value&page.index=0&page.size=10

Response body

By default, transcoding serializes the entire response message as JSON. The response_body field allows serialization of a subset of the response message.

[!code-protobuf]

In the preceding example, the address field is serialized to the response body as JSON.

Specification

For more information about customizing gRPC transcoding, see the HttpRule specification.

Customize JSON

Messages are converted to and from JSON using the JSON mapping in the Protobuf specification. Protobuf's JSON mapping is a standardized way to convert between JSON and Protobuf, and all serialization follows these rules.

However, gRPC JSON transcoding offers some limited options for customizing JSON with xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings, as shown in the following table.

Option Default Value Description
xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.IgnoreDefaultValues false If set to true, fields with default values are ignored during serialization.
xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteEnumsAsIntegers false If set to true, enum values are written as integers instead of strings.
xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteInt64sAsStrings false If set to true, Int64 and UInt64 values are written as strings instead of numbers.
xref:Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.WriteIndented false If set to true, JSON is written using pretty printing. This option doesn't affect streaming methods, which write line-delimited JSON messages and can't use pretty printing.
builder.Services.AddGrpc().AddJsonTranscoding(o =>
{
    o.JsonSettings.WriteIndented = true;
});

In the .proto file, the json_name field option customizes a field's name when it's serialized as JSON, as in the following example:

message TestMessage {
  string my_field = 1 [json_name="customFieldName"];
}

Transcoding doesn't support advanced JSON customization. Apps requiring precise JSON structure control should consider using ASP.NET Core Web API.

Additional resources