Click here to Skip to main content
15,881,413 members
Articles / Hosted Services / Azure
Tip/Trick

Dealing with Optional Content in an Azure Function

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Nov 2020CPOL 3.3K   1   1
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)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
QuestionMessage Closed Pin
27-Feb-21 22:37
Member 1508594027-Feb-21 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.