Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Dealing with Optional Content in an Azure Function

5.00/5 (1 vote)
17 Nov 2020CPOL 3.7K  
Fix the 500 error if no Content-Type is specified
In this post, you will see how to deal with optional content in an Azure function.

Introduction

If you call an Azure function that has Content.ReadAsAsync with no Content-Type header, it will fail with a 500 error. This allows you to skip over that if no content is specified.

Using the Code

In the Azure function body, put the ReadAsAsync in two nested null traps thus:

C#
ExistingBalanceData priorBalance = null;

if (req.Content != null)
      {
          if (! (req.Content.Headers.ContentType == null))
          {
              priorBalance = await req.Content.ReadAsAsync<ExistingBalanceData>();
          }
      }

The first will deal with cases where no content is sent, the second where no Content-Type header has been set - some clients seem to do this to indicate that there is no content.

History

  • 17th November, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)