Can the Azure Storage REST API send the response in JSON format?

Viewed 469

I am developing a library for working with various types of cloud based queue services. One of those services is the Azure Queue Storage REST API.

For the Amazon SQS service I can send an Accept: application/json header and the response is in JSON format.

Since JSON is a format that is supported by many APIs and XML is not fun to work with, I would prefer the Azure Storage REST API to also return a response in JSON format.

I have tried to set the Accept: application/json header to no avail. The responses are all in XML format with Content-Type: application/xml, which is obviously not what I was asking for.

Currently all code is in C with dependencies on a couple of libraries, including cURL and jansson, although for this question that doesn't really matter. It's just that I would like the library to be as simple and lightweight as possible.

I have a hard time digging through all kinds of documentation. Most topics I can find are about sending JSON within a message. But that's not what I'm going for.

Is it even possible to receive the actual responses in JSON? I would really like to drop my libxml2 dependency.

1 Answers

As pointed by @Tom Because the documentation is stating that it only return XML, I would personally write an azure function who becomes an adaptor which basically takes your request, sends it to azure queue storage, retrieves the xml response and then converts the xml response to json and return the json to the caller (which will be your C code).

A sample python code to convert xml to json is shown below:

import xmltodict
import json

text = ''

xpars = xmltodict.parse(text)
json = json.dumps(xpars)
print(json)

A sample xml message

text = '<QueueMessagesList>  
    <QueueMessage>  
      <MessageId>string-message-id</MessageId>  
      <InsertionTime>insertion-time</InsertionTime>  
      <ExpirationTime>expiration-time</ExpirationTime>  
      <PopReceipt>opaque-string-receipt-data</PopReceipt>  
      <TimeNextVisible>time-next-visible</TimeNextVisible>  
      <DequeueCount>integer</DequeueCount>  
      <MessageText>message-body</MessageText>  
    </QueueMessage>  
</QueueMessagesList>'

And the response will be :

{
  "QueueMessagesList": {
    "QueueMessage": {
      "MessageId": "string-message-id",
      "InsertionTime": "insertion-time",
      "ExpirationTime": "expiration-time",
      "PopReceipt": "opaque-string-receipt-data",
      "TimeNextVisible": "time-next-visible",
      "DequeueCount": "integer",
      "MessageText": "message-body"
    }
  }
}

Please Note: This whole thing can also be done using a Logic App in azure.
I have only shown the XML to JSON converter part here, but it is really straightforward to write an HTTP Trigger Azure Function to do the same. Or you can even write this converter into your C code as well.

Hope this helps you in moving on with your library development.

Related