How to troubleshoot Azure Functions HTTP Trigger 404 Error?
Published Mar 17 2023 05:12 AM 27K Views
Microsoft

Overview

Azure Functions is a serverless compute service that allows you to run code on-demand without having to manage infrastructure. It supports various types of triggers, including HTTP triggers, which allow you to expose your function as a RESTful API.

However, while working with Azure Function HTTP triggers, you may encounter a 404 error, which indicates that the requested resource was not found.

In this blog, we will learn the possible causes of the Azure Function HTTP trigger 404 error, along with examples to help you troubleshoot and resolve the issue.

 

Causes of Azure Function HTTP Trigger 404 Error

 

The Azure Function HTTP trigger 404 error can occur due to various reasons, including:

 

  • Incorrect Function Name or URL Path

One of the common causes of the Azure Function HTTP trigger 404 error is an incorrect function name or URL path. The function name and URL path should match the values specified in the function code and function.json file. Any mismatch in the function name or URL path can result in a 404 error. This could be due to a typo in the URL.

 

To solve this issue, navigate to one of your HTTP-triggered functions in the Azure portal and select Get function URL.

 

 

GetFunctionURL.png

 

Another scenario where you might encounter a 404 error despite using the correct URL and function name could be related to the ROUTE property.

 

Example:

Suppose you have an Azure Function HTTP trigger with the following function code:

 

public static async Task<IActionResult> Run(

    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "hello")] HttpRequest req,

    ILogger log)

{

    log.LogInformation("C# HTTP trigger function processed a request.");

    return new OkObjectResult("Hello, World!");

}

 

By default, when you create a function for an HTTP trigger, the function is addressable with a route of the form:

 

http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>

 

In the function code above, the HTTP trigger is configured to respond to a GET request with the URL path "/hello". However, if you try to access the function with a different URL path, such as "/world", you will receive a 404 error.

 

One more example is, the following function.json file defines a route property for an HTTP trigger with two parameters, category and id:

 

{
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"methods": [ "get" ],
"route": "products/{category:alpha}/{id:int?}"
},
{
"type": "http",
"name": "res",
"direction": "out"
}
]
}

 

Using this configuration, the function is now addressable with the following route instead of the original route.

 

http://<APP_NAME>.azurewebsites.net/api/products/electronics/123

 

Accessing default URL will result 404.

 

Refer: Customize the HTTP endpoint 

 

  • Invalid Request Method

The Azure Function HTTP trigger supports only certain HTTP methods, including GET, POST, PUT, DELETE, and PATCH. If you use an invalid HTTP method in your request, the function will return a 404 error.  

HTTP Methods property in azure function is “An array of the HTTP methods to which the function responds. If not specified, the function responds to all HTTP methods.”

You must specify the http method at the time of calling http function.

 

Suppose you have an Azure Function HTTP trigger that only supports POST requests. If you try to access the function with a GET request, you will receive a 404 error. Same explained in below example.

 

Http trigger supports only POST method,

 

Http Method.jpg

Getting 404 when called through browser via GET request.

 

Http get 404.jpg

  • Http Trigger function is disabled.

Calling a disabled http trigger function will result 404 therefore check if function is disabled or not before calling it.

 

Refer: Disabled Function 

 

Similar to this if you have functions property in host.json which describes a list of functions that the job host runs (An empty array means run all functions.) and you are calling a function which is not listed inside this will result in 404 error.

 

{
"functions": [ "function1", "function2" ]
}

 

  • Custom response from code

Here's an example of an HTTP trigger function in C# that returns a 404 error if the name parameter is missing:

 

public static class HelloWorld

{

    [FunctionName("HelloWorld")]

    public static async Task<IActionResult> Run(

        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)

    {

        string name = req.Query["name"];

        if (string.IsNullOrEmpty(name))

        {

            return new NotFoundResult();

        }

        return new OkObjectResult($"Hello, {name}");

    }

}

 

If the name parameter is missing, this function will return a 404 error with the message "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

 

Please validate the code if it is explicitly returning a 404 error. 

 

  • Diagnose and solve problems

When you’re running a function app, you want to be prepared for any issues that may arise, from 404 errors to trigger failures.

Azure Functions diagnostics is an intelligent and interactive experience to help you troubleshoot your function app with no configuration or extra cost. When you do run into issues with your function app, Azure Functions diagnostics points out what’s wrong.

It guides you to the right information to more easily and quickly troubleshoot and resolve the issue. 

  • Navigate to your function app in the Azure portal.
  • Select Diagnose and solve problems to open Azure Functions diagnostics.
  • Choose a category that best describes the issue of your function app, like in current case it is "HTTP 4XX Errors"

 

Diagnose and solve problems.png

 

This will not only display information about HTTP 404 error, but also list down details for other 4XX HTTP errors, such as:

  • 400 Bad Request
  • 401 Unauthorized
  • 403 Forbidden

 

 

Note: If you have set an authorization level other than "anonymous," you need to specify the function access key in the URL.

 

Example: https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>

 

If you specify the wrong access key, you may expect a 404 error because it forms an incorrect URL. However, it actually returns a 401 error, indicating that you are unauthorized to access the requested resource.

 

 

Conclusion

HTTP 404 errors can be difficult when working with Azure Functions, but they are often easy to diagnose and fix. By following the steps outlined in this article, you should be able to troubleshoot and fix HTTP 404 errors in your Azure Functions.

 

References

Azure Functions HTTP triggers and bindings | Microsoft Learn

 

 

3 Comments
Co-Authors
Version history
Last update:
‎Mar 20 2023 12:37 AM
Updated by: