Proxies

Using proxies.json

Azure Functions Proxies can be used to modify requests to and responses from your function app. Simpler configurations can be managed from the Azure portal, which modifies a file called proxies.json behind the scenes. You can modify proxies.json directly to enable more complex scenarios.

The App Service Editor allows you to modify all files in the function app's root directory. You can use it to edit an existing proxies.json file. If a proxies.json does not exist, you can use it to create one and edit it.

Takeaways

  • Azure Functions Proxies are configured using proxies.json.
  • You can edit proxies.json using App Service Editor.

Read more

Creating an HTTP Redirect

Azure Functions Proxies can be configured to return a custom response. You can use it to return custom HTTP status codes and headers. For a permanent redirect, respond with a 301 status code. For a temporary redirect, respond with 302. Indicate the URL to redirect to in the Location header.

To configure proxies, edit proxies.json.

{
  "proxies": {
    "permanent redirect": {
      "matchCondition": {
        "methods": [
          "GET"
        ],
        "route": "/permanent"
      },
      "responseOverrides": {
        "response.statusCode": "301",
        "response.headers.Location": "https://docs.microsoft.com/"
      }
    },
    "temporary redirect": {
      "matchCondition": {
        "methods": [
          "GET"
        ],
        "route": "/temporary"
      },
      "responseOverrides": {
        "response.statusCode": "302",
        "response.headers.Location": "https://docs.microsoft.com/"
      }
    }
  }
}

Takeaways

  • Azure Functions Proxies are configured using proxies.json.
  • Proxies can be used to set the status code and headers of an HTTP response.

Read more

Creating a mock API

Azure Functions Proxies can return custom responses without a backend service. It can be used to create a mock API.

Here is the proxies.json of a mock API that responds to GET and POST requests.

{
  "proxies": {
    "Mock API - GET": {
      "matchCondition": {
        "methods": [
          "GET"
        ],
        "route": "/api/v1/orders/{orderId}"
      },
      "responseOverrides": {
        "response.statusCode": "200",
        "response.headers.Content-Type": "application/json",
        "response.body": {
          "id": "{orderId}",
          "total": 100,
          "items": [
            {
              "item": "2e649ae3-09f2-474a-bec6-0d066d32cd0a",
              "quantity": 5,
              "price": 20
            }
          ]
        }
      }
    },
    "Mock API - POST": {
      "matchCondition": {
        "methods": [
          "POST"
        ],
        "route": "/api/v1/orders"
      },
      "responseOverrides": {
        "response.statusCode": "201",
        "response.headers.Location": "https://%WEBSITE_HOSTNAME%/api/v1/orders/d0458144-407c-4f05-aa8d-90102c488603"
      }
    }
  }
}

Takeaways

  • Proxies can be used to set the status code, headers, and body of a response.
  • Proxies can be configured to without a backendUri.

Read more

Returning conditional responses based on backend response codes

Azure Functions Proxies can be configured to return a custom response based on the backend response.

To configure proxies, edit proxies.json.

{
  "proxies": {
    "statuses": {
      "matchCondition": {
        "methods": [
          "GET"
        ],
        "route": "/status/{code}"
      },
      "backendUri": "https://httpbin.org/status/{code}",
      "responseOverrides": {
        "backend.response.statusCode": {
          "401": {
            "response.statusCode": 302,
            "response.headers.Location": "https://myloginpage"
          },
          "418": {
            "response.headers.Content-Type": "text/plain",
            "response.body": "I'm a teapot"
          },
          "5xx": {
            "response.headers.Content-Type": "application/json",
            "response.body": {
              "statusCode": "{backend.response.statusCode}"
            }
          }
        }
      }
    }
  }
}

Takeaways

  • Azure Functions Proxies are configured using proxies.json.
  • Proxies can customize the response based on the backend response status code.

Read more

Referencing application settings

Azure Functions Proxies configurations can reference application settings and other environment variables using % signs.

To configure proxies, edit proxies.json.

{
  "proxies": {
    "statuses": {
      "matchCondition": {
        "methods": [
          "GET"
        ],
        "route": "/test/{id}"
      },
      "backendUri": "https://%WEBSITE_HOSTNAME%/{id}",
      "responseOverrides": {
        "response.statusCode": "200",
        "response.headers.Content-Type": "application/json",
        "response.body": {
          "id": "{id}",
          "value": "%DEFAULT_VALUE%"
        }
      }
    }
  }
}

Takeaways

  • Azure Functions Proxies are configured using proxies.json.
  • Proxies configuration can reference environment variables and application settings.

Read more