Is the Azure Function response status code always HTTP 200? Did you know?
Is the Azure Function response status code always HTTP 200? Did you know?
• Azure Functions, Serverless, .NET, Azure
• 2 min read
Is the Azure Function response status code always HTTP 200? Can I change that? It depends on your implementation.
The issue
Most ASP.NET developers (including myself) would leverage IActionResult in API responses, and that’s where the issue lies.
What happens is that no matter the object that you use, which implements IActionResult, you always get an HTTP 200 response in Azure Functions.
Look at the endpoints below, for the first request OkStatusCode you should get a HTTP 200 as it returns a OkObjectResult, what about AlwaysHttp200StatusCode? It returns a BadRequestObjectResult, which in theory should be reflected as an HTTP 400 response. Is that the case?
What about AlwaysHttp200StatusCodeAgain? That’s an InternalServerErrorResult!
[Function(nameof(OkStatusCode))]publicIActionResultOkStatusCode([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");returnnewOkObjectResult("Welcome to Azure Functions!");}[Function(nameof(AlwaysHttp200StatusCode))]publicIActionResultAlwaysHttp200StatusCode([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");returnnewBadRequestObjectResult("Welcome to Azure Functions!");}[Function(nameof(AlwaysHttp200StatusCodeAgain))]publicIActionResultAlwaysHttp200StatusCodeAgain([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");returnnewInternalServerErrorResult();}
Funny enough we get HTTP 200 responses with a body that lists the different statuses.
The HTTP response
The problem above happens because we are manipulating the response body payload and not the HTTP response.
To be able to modify the HTTP response, we need to use an object that changes Http responses, using HttpResponseData for example.
[Function(nameof(OkHttpStatusCode))]publicHttpResponseDataOkHttpStatusCode([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");varresponse=req.CreateResponse(HttpStatusCode.OK);response.WriteString("Welcome to Azure Functions!");returnresponse;}[Function(nameof(BadRequestHttpStatusCode))]publicHttpResponseDataBadRequestHttpStatusCode([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");varresponse=req.CreateResponse();response.WriteString("Welcome to Azure Functions!");response.StatusCode=HttpStatusCode.BadRequest;returnresponse;}[Function(nameof(InternalErrorHttpStatusCode))]publicHttpResponseDataInternalErrorHttpStatusCode([HttpTrigger(AuthorizationLevel.Function,"get","post")]HttpRequestDatareq){_logger.LogInformation("C# HTTP trigger function processed a request.");varresponse=req.CreateResponse(HttpStatusCode.InternalServerError);response.WriteString("Welcome to Azure Functions!");returnresponse;}
When running those endpoints this is what we get. What we expect!
Join the conversation! Share your thoughts and connect with other readers.