0
votes

I am sending event to IoT hub using azure sdk and rest client.

I have to use IoT hub routing features so I am adding addition property to main payload before sending to IotHub

Case 1: Sending Data using Azure SDK

Event Model

RealTimeMachineData realTimeData = new RealTimeMachineData();
realTimeData.Date = DateTime.UtcNow;          
realTimeData.MachineCode = "M1";

SDK code to send data to IoT Hub enter image description here

When I see this message in Service bus explorer i look like this

{
  "MachineCode": "M1",
  "Date": "2017-08-27T10:05:22.7063498Z",  
}

Case 2: When I do the same thing by calling through Rest API

 HttpClient client = new HttpClient();

 string deviceId = "DemoDevice";
 string baseUrl = "https://******.azure-devices.net/devices/" + deviceId + "/messages/events?api-version=2016-02-03";
 client.BaseAddress = new Uri(baseUrl);

 var sas = @"SharedAccessSignature sr=********";
 client.DefaultRequestHeaders.Add("Authorization", sas);
 client.DefaultRequestHeaders.Accept.Add(new  MediaTypeWithQualityHeaderValue("application/json"));

   var message1 = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(realTimeData.ToString()));
 message1.Properties.Add("Source", "AiR");


 HttpResponseMessage response = client.PostAsJsonAsync(baseUrl, message1).Result;

After sending event this way when i check the message to service bus explorer it look like

enter image description here

I have to use this approach in stream analytics and since the payload is getting different when sending through rest client so facing lot of problems.

Am I doing something wrong or is there something different I have to do while sending via Rest client?

1

1 Answers

0
votes

the following is the fix:

var message1 = new StringContent(JsonConvert.SerializeObject(new RealTimeMachineData() { Date = DateTime.UtcNow, MachineCode = "M1" }));
client.DefaultRequestHeaders.Add("iothub-app-Source", "AiR");