Introduction
Applications that communicate with AWS services often receive structured responses containing resource details, configuration values, metrics, status information, and other application data. Traditionally, many AWS APIs have used JSON or XML to exchange this information. However, AWS is also introducing support for Smithy RPC v2 CBOR, a binary protocol designed to make communication more efficient.
CBOR stands for Concise Binary Object Representation. Instead of representing structured information as readable text, it encodes values into a compact binary format. This can reduce the amount of data transferred between an application and an AWS service and can also reduce serialization overhead.
For .NET developers, this introduces an important distinction. When using an AWS SDK service client that supports CBOR, the SDK handles the protocol and response deserialization internally. Developers generally continue working with normal C# response objects. However, when an application receives a raw CBOR payload through a custom integration or another interface, it may need to decode the binary data itself.
In this article, we will explore how AWS uses CBOR, how to work with CBOR-enabled AWS SDK clients in C#, and how to decode a CBOR response using System.Formats.Cbor.
Understanding CBOR Responses From AWS
AWS uses different wire protocols depending on the service and API operation. A wire protocol defines how a client sends requests and receives responses.
Some commonly used AWS protocols include:
| Protocol | Description |
|---|---|
| AWS JSON | Uses JSON for structured requests and responses |
| AWS Query | Uses XML-based request and response structures |
| REST JSON | Uses HTTP methods, resource paths, and JSON payloads |
| Smithy RPC v2 CBOR | Uses a compact binary CBOR representation |
Smithy RPC v2 CBOR is designed to provide more efficient communication between AWS services and supported SDK clients.
For example, AWS Cloud Map has introduced support for this protocol alongside its existing AWS JSON protocol. Amazon CloudWatch has also introduced support for newer JSON and CBOR protocols.
The important point is that CBOR support is service-specific. Not every AWS service or API operation uses CBOR, and an application should not assume that every AWS response contains binary data.
AWS SDKs manage the supported protocol automatically when the service client and SDK version provide that capability.
How AWS SDK for .NET Handles CBOR
When an application uses an AWS SDK service client, the SDK handles several communication tasks internally:
- It prepares the API request.
- It selects a supported wire protocol.
- It sends the request to the AWS service.
- It receives the response.
- It deserializes the response into the corresponding C# response model.
This means application code usually does not need to create a CborReader or manually inspect response bytes when using a supported AWS SDK operation.
For example, an application using the AWS CloudWatch SDK can continue accessing metrics through strongly typed response objects, even when the SDK uses a binary protocol internally.
The AWS SDK for .NET v4 includes support for Smithy RPC v2 CBOR for supported services. The CloudWatch protocol documentation identifies AWS SDK for .NET v4 version 4.0.150.0 as a supported release for its newer protocol support.
Always check the protocol support information for the specific AWS service you use. Having the latest SDK does not mean every service automatically uses CBOR.
Installing the AWS SDK for .NET
To communicate with Amazon CloudWatch, install its SDK package:
dotnet add package AWSSDK.CloudWatchFor a project that needs to decode raw CBOR payloads independently, install the CBOR package as well:
dotnet add package System.Formats.CborThe AWS SDK package provides the service-specific client and response models. System.Formats.Cbor provides the reader and writer APIs for working directly with CBOR-encoded data.
These packages serve different purposes. The AWS SDK manages AWS service communication, while System.Formats.Cbor is useful when application code needs direct access to a CBOR payload.
Calling an AWS Service Using the SDK
Let's start with a CloudWatch example.
The following code creates a CloudWatch client and retrieves metrics for a specified namespace.
using Amazon;
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
var client = new AmazonCloudWatchClient(
RegionEndpoint.USEast1);
var request = new ListMetricsRequest
{
Namespace = "MyApplication"
};
ListMetricsResponse response =
await client.ListMetricsAsync(request);
foreach (var metric in response.Metrics)
{
Console.WriteLine(
$"Metric: {metric.MetricName}");
Console.WriteLine(
$"Namespace: {metric.Namespace}");
}The application creates an AmazonCloudWatchClient and sends a ListMetrics request.
The SDK handles the communication with CloudWatch and converts the service response into a ListMetricsResponse object.
Notice that the application does not need to know whether the response was transported using JSON or CBOR. It continues accessing the returned metrics through normal C# properties.
This is one of the main advantages of using an AWS SDK instead of implementing the service protocol manually.
When Should You Decode CBOR Manually?
Manual decoding is useful when the application receives CBOR data outside the normal AWS SDK response-processing flow.
For example, consider an integration where an AWS-related component publishes a CBOR payload through an MQTT message, or a custom service returns a documented CBOR response.
In these situations, the application receives binary data rather than an already deserialized C# object.
The application needs to understand the payload structure before it can access individual values.
For this example, assume a documented application payload uses the following logical structure:
{
"deviceId": "DEVICE-1001",
"temperature": 29.5,
"active": true,
"timestamp": 1795594200
}The actual response is encoded as a CBOR map rather than JSON text.
We can use CborReader to decode this payload.
Reading a CBOR Payload Using CborReader
First, define a model to hold the decoded response.
public sealed record DeviceResponse(
string DeviceId,
double Temperature,
bool IsActive,
long Timestamp);Next, create a method that reads the CBOR map.
using System.Formats.Cbor;
static DeviceResponse DecodeResponse(byte[] payload)
{
var reader = new CborReader(
payload,
CborConformanceMode.Strict);
int? mapLength = reader.ReadStartMap();
if (mapLength is null || mapLength > 20)
{
throw new InvalidDataException(
"Invalid response structure.");
}
string? deviceId = null;
double temperature = 0;
bool isActive = false;
long timestamp = 0;
for (int i = 0; i < mapLength.Value; i++)
{
string key = reader.ReadTextString();
switch (key)
{
case "deviceId":
deviceId = reader.ReadTextString();
break;
case "temperature":
temperature = reader.ReadDouble();
break;
case "active":
isActive = reader.ReadBoolean();
break;
case "timestamp":
timestamp = reader.ReadInt64();
break;
default:
reader.SkipValue();
break;
}
}
reader.ReadEndMap();
if (reader.BytesRemaining != 0)
{
throw new InvalidDataException(
"Unexpected data after the CBOR response.");
}
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new InvalidDataException(
"Device identifier is missing.");
}
return new DeviceResponse(
deviceId,
temperature,
isActive,
timestamp);
}The method starts by creating a CborReader in strict conformance mode. It then reads the beginning of the CBOR map and checks that the structure is within the expected size.
Each map entry contains a key and a corresponding value. The switch statement identifies the key and reads the value using the appropriate reader method.
For example, ReadTextString reads the device identifier, ReadDouble reads the temperature, and ReadBoolean reads the active status.
The default case uses SkipValue to ignore unknown properties. This can help when a response adds optional fields that the current application does not need.
After reading the map, the method checks for unexpected trailing bytes and validates the required device identifier.
This example assumes a definite-length map and the documented value types. A real integration should follow the exact CBOR schema provided by the service.
Receiving a CBOR Response Through HttpClient
Some applications communicate with custom endpoints rather than using a service-specific AWS SDK client. If the endpoint explicitly documents CBOR responses, HttpClient can receive the binary response.
The following example shows how to request a CBOR representation from such an endpoint.
using System.Net.Http;
using var httpClient = new HttpClient();
using var request = new HttpRequestMessage(
HttpMethod.Get,
"https://api.example.com/device/status");
request.Headers.Accept.ParseAdd("application/cbor");
using HttpResponseMessage response =
await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
if (response.Content.Headers.ContentType?.MediaType
!= "application/cbor")
{
throw new InvalidDataException(
"The endpoint did not return CBOR data.");
}
byte[] payload =
await response.Content.ReadAsByteArrayAsync();
DeviceResponse device =
DecodeResponse(payload);
Console.WriteLine(
$"Device: {device.DeviceId}");
Console.WriteLine(
$"Temperature: {device.Temperature}");The Accept header indicates that the client prefers a CBOR response. However, the server must explicitly support this representation. Adding the header does not force an AWS service or custom endpoint to return CBOR.
The application also checks the response Content-Type before passing the bytes to the decoder.
This example is intended for a custom endpoint with a documented standard CBOR response. It should not be used to decode the internal Smithy RPC v2 CBOR wire representation returned by an AWS SDK operation.
Understanding Smithy RPC v2 CBOR and Standard CBOR
An important mistake is assuming that every CBOR response from AWS can be decoded as an ordinary CBOR map using CborReader.
CBOR is the underlying binary representation, but Smithy RPC v2 CBOR defines additional protocol rules for representing service requests and responses.
These rules are part of the AWS service protocol and are not necessarily equivalent to a custom application map containing property names and values.
For this reason, use the AWS SDK when communicating with a supported AWS service. The SDK understands the service model and protocol-specific encoding and decoding rules.
Use System.Formats.Cbor directly when the application receives a standard CBOR payload whose structure is documented and available to your code.
This distinction prevents unnecessary protocol implementation work and reduces the risk of incorrectly interpreting AWS service responses.
Best Practices for Working With AWS CBOR Responses
Use supported SDK versions
Check the AWS SDK for .NET version required by the service. For services that support Smithy RPC v2 CBOR, use a compatible SDK release.
Prefer strongly typed service responses
When an AWS SDK provides a service client and response model, use them instead of reading the underlying HTTP response manually.
Validate response content types
When using custom HTTP integrations, verify that the server actually returned application/cbor before attempting to decode the payload.
Follow the service schema
Do not assume that a CBOR map always contains the same keys or value types. Use the service's documented response structure.
Handle unknown fields carefully
A response may evolve as new fields are introduced. Decide whether unknown values should be skipped or treated as errors based on the application's compatibility requirements.
Protect against malformed payloads
Set reasonable response size and nesting-depth limits. Handle CborContentException and other decoding errors, and validate decoded values before using them.
Avoid unnecessary conversions
Do not convert a binary CBOR response into JSON simply to read a few values. Decode the supported structure directly when manual decoding is appropriate.
Common Mistakes to Avoid
One common mistake is assuming that every AWS service supports CBOR. Protocol support depends on the service, operation, SDK version, and sometimes the service configuration.
Another mistake is manually decoding a response that the AWS SDK already processes. This adds unnecessary complexity and can introduce compatibility problems when the service protocol changes.
Developers may also confuse standard CBOR with Smithy RPC v2 CBOR. Although both use CBOR encoding, the AWS protocol includes additional rules that application code must not ignore.
Finally, checking only the HTTP status code is not enough when using custom integrations. The application should also verify the response content type, validate the binary structure, and handle decoding failures.
Advantages and Disadvantages
Advantages
- CBOR can reduce the size of structured service responses.
- Binary encoding can reduce text parsing overhead.
- Supported AWS SDK clients handle protocol processing automatically.
- System.Formats.Cbor provides APIs for custom CBOR integrations.
- Strongly typed AWS SDK models keep application code independent of the underlying wire format.
Disadvantages
- CBOR responses are harder to inspect manually than JSON.
- Protocol support varies between AWS services.
- Custom integrations require knowledge of the response schema.
- Manual decoding introduces additional validation and maintenance work.
- Smithy RPC v2 CBOR should not be treated as an ordinary application-defined CBOR map.
Summary
CBOR is becoming an important option for efficient communication between AWS services and supported SDK clients. Smithy RPC v2 CBOR provides a compact binary protocol that can reduce payload sizes and serialization overhead compared with traditional text-based formats.
For .NET developers, the simplest approach is to use a compatible AWS SDK service client. The SDK handles protocol selection and response deserialization, allowing application code to continue working with strongly typed C# models.
When a custom integration receives a raw CBOR payload, System.Formats.Cbor provides CborReader for decoding the data. The application must follow the documented response schema and validate the decoded values.
The key is to choose the correct approach for the integration. Use the AWS SDK for AWS service operations and direct CBOR decoding only when the application is responsible for processing a documented CBOR payload.

Join the conversation! Your thoughts help the community grow.