I recently encountered a situation where I had to use pure REST Calls to send data to an Azure Event Hub.
Tips:
- If you are used to using libraries (C#, Python) you will find that the libraries are doing a lot behind the scenes. Its not trivial to go from using the library to making pure REST calls
- The first approach – using Fiddler to capture the traffic and re-purpose those calls – failed.
- I am not sure why the calls fail to show up on fiddler. I tried out a few things like decrypt HTTPS and stuff. But I wasn’t able to get the sending traffic to show up on Fiddler
- The references below give a good of how I made some progress.
REST Call to send data:
I finally got it to work with something like this:
POST https://simplexagpmeh.servicebus.windows.net/simplexagpmeh/messages?timeout=60&api-version=2014-01 HTTP/1.1 User-Agent: Fiddler Authorization: SharedAccessSignature sr=http%3a%2f%2fsimplexagpmeh.servicebus.windows.net%2f&sig=RxvSkhotfGEwERdiaA8oLr7X9u5XLeDI8TCK5DhDPP8%3d&se=1476214239&skn=RootManageSharedAccessKey ContentType: application/atom+xml;type=entry;charset=utf-8 Host: simplexagpmeh.servicebus.windows.net Content-Length: 153 Expect: 100-continue { "DeviceId" : "ArduinoYun", "SensorData" : [ { "SensorId" : "awk", "SensorType" : "temperature", "SensorValue" : 24.5 } ] }
References:
- http://www.jamessturtevant.com/posts/using-azure-python-sdk-with-event-hubs/
- this link shows how to use Python library for sending data to EventHub.
- https://blog.kloud.com.au/2014/10/11/the-internet-of-things-with-arduino-azure-event-hubs-and-the-azure-python-sdk/
- this link gave some hope 🙂
- in particular, it has a snippet of a REST request which told me it could be done.
- i also realised at least is that I needed to find out about SharedAccessSignatures
- https://azure.microsoft.com/en-us/documentation/articles/service-bus-shared-access-signature-authentication/
- http://developers.de/blogs/damir_dobric/archive/2013/10/17/how-to-create-shared-access-signature-for-service-bus.aspx
- I used this to build a C# app to generate the SharedAccessSignature. The SharedAccessSignature is used in the ‘Authorization’ header above.
- http://fabriccontroller.net/iot-with-azure-service-bus-event-hubs-authenticating-and-sending-from-any-type-of-device-net-and-js-samples/
- https://www.sepago.com/blog/2016/03/16/generate-a-sas-token-to-access-microsoft-azure-event-hub-PoSH
- the 4 links above talk about use of SharedAccessSignatures.
Code:
[…] I recently had to make REST calls in Python for sending data to Azure EventHub. […]