This is a quick blog post to document my experience with building serverless
web APIs with Azure Functions. Recently, I was building an API where I
wanted to receive an XML message in the body of the HTTP trigger, but I did
not want the Functions framework to attempt to deserialize the data. I
wanted to receive the raw input so I could pass it as-is to another API.
When you create a new Azure Function, the default template will use the
following statement to read the body of the HTTP request:
This statement will attempt to deserialize the HTTP request body by using the
Content-Type header to choose a serializer component. By default it supports
the JSON and XML serializers, but you can define and register your own custom
serializer too. In order for the serializer to work properly, you need to have
a classs with the appropriate decorators so the serializer knows how to map
the data to the class properties.
In my use case, I was not going to do anything with the data other than pass
it on to another API, so I did not want to incur the overhead of
deserializing/serializing the data. Instead, you can change the above
statement to read the body as a string. When reading the data as a string,
the framework will not attempt to deserialzie the data.
To read the body as a string, replace the above statement with this:
It turns out getting the raw input is nothing more than a simple statement
change. All it takes is changing the statement from reading the data as an
object to reading it as a string. Now I have it documented for the next time
I need to do something similar in Azure Functions.
This article originally posted at https://scottbanwart.com/blog/2017/07/azure-functions-raw-input