Skip to content

Commit 663f834

Browse files
set ContentType and -Encoding on telemetry (#333)
1 parent f54d1d0 commit 663f834

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

shared/src/azure_iot_nx_client.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
#define TELEMETRY_BUFFER_SIZE 256
3838
#define PROPERTIES_BUFFER_SIZE 128
3939

40+
// define static strings for content type and -encoding on message property bag
41+
static const UCHAR content_type_property[] = "$.ct";
42+
static const UCHAR content_encoding_property[] = "$.ce";
43+
static const UCHAR content_type_json[] = "application%2Fjson";
44+
static const UCHAR content_encoding_utf8[] = "utf-8";
45+
4046
static UCHAR telemetry_buffer[TELEMETRY_BUFFER_SIZE];
4147
static UCHAR properties_buffer[PROPERTIES_BUFFER_SIZE];
4248

@@ -666,6 +672,28 @@ UINT azure_iot_nx_client_publish_telemetry(AZURE_IOT_NX_CONTEXT* context_ptr,
666672
return status;
667673
}
668674

675+
// set the ContentType property on the message to "application/json" (url-encoded)
676+
status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr, content_type_property, sizeof( content_type_property )-1,
677+
content_type_json, sizeof( content_type_json )-1,
678+
NX_WAIT_FOREVER);
679+
if (status != NX_AZURE_IOT_SUCCESS)
680+
{
681+
printf("Error: cannot set ContentType message property (0x%08X)\r\n", status);
682+
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
683+
return status;
684+
}
685+
686+
// set the ContentEncoding property on the message to "utf-8"
687+
status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr, content_encoding_property, sizeof( content_encoding_property )-1,
688+
content_encoding_utf8, sizeof( content_encoding_utf8 )-1,
689+
NX_WAIT_FOREVER);
690+
if (status != NX_AZURE_IOT_SUCCESS)
691+
{
692+
printf("Error: Canot set ContentEncoding message property (0x%08X)\r\n", status);
693+
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
694+
return status;
695+
}
696+
669697
telemetry_length = nx_azure_iot_json_writer_get_bytes_used(&json_writer);
670698
if ((status = nx_azure_iot_hub_client_telemetry_send(
671699
&context_ptr->iothub_client, packet_ptr, telemetry_buffer, telemetry_length, NX_WAIT_FOREVER)))

0 commit comments

Comments
 (0)