Pass Parameters To Azure Function From Data Factory: Body, Query, Headers, Path
Azure Function, also known as Function App, is a popular Azure service that is part of the serverless stack. It allows you to run small pieces of code or “functions” in a highly scalable manner without worrying about the underlying infrastructure.
Azure Functions are used with Azure Data Factory (ADF) for a variety of reasons. For example, Azure Functions can perform specific data transformations or enrichments that cannot be done within Data Factory itself. Additionally, Azure Function can be used to trigger other services or applications as part of ADF pipeline execution.
Azure Function parameters can be passed from Data Factory using query string, path, request body, or headers. The choice of how to pass parameters depends on the implementation of the Azure Function and the HTTP method used to invoke the endpoint.
What You Will Learn:
- How to pass parameters in Body, Query, Path, or Headers from Data Factory to Azure Function.
- How to call Function App in Data Factoy: Azure Function and Web activities in Data Factory.
- How to set up and use different HTTP methods in Azure Function.
Contents:
- Overview
- Prerequisites
- Connecting To Azure Function
- 1. Body Parameters
- 2. Query Parameters
- 3. Path Parameters
- 4. Header Parameters
- HTTP Methods: GET, POST, PUT, DELETE, & Others
- Summary
- Related Posts
- Useful Links
Overview
Azure Function can be invoked as an HTTP endpoint, where the caller passes input parameters as part of the HTTP message, and Azure Function does some work and returns result.
Now, if we put Azure Functions in the context of Azure Data Factory, we have a question of how to integrate these two together - the answer is to use Azure Function or Web activity available in Data Factory.
Additionally, in most cases, we want to pass some data (aka parameters or arguments), to the invoked Azure Function. For this, we can use mechanisms provided by the HTTP protocol: request body, query string, request path, or headers.
In the following sections, we cover how to pass parameters as part of the request body, query, path, or header. And then wrap up the conversation by discussing the use of HTTP methods in Azure Functions.
NOTE: Azure Function and Function App are used as synonyms in this post.
Prerequisites
The examples in this post assume that we have the following Azure resources created. Please familiarize yourself with them, but you don’t necessarily need to have these resources to follow the post.
-
Azure Function with HTTP trigger and HTTP output binding. Or in other words, an Azure Function which acts as an HTTP endpoint - the functions gets triggered through an HTTP request and returns a response as a result. The function can be written in any programming language supported by the Azure environment. Examples in this post assume that the Function App name is
func-contoso
, and the function name isFunction1
. For this post, I created a default Python function in the portal using HTTP trigger template. -
Azure Data Factory resource with a pipeline in it, here we’ll use Azure Function Activitity or Web Activity to call the function and receive results. Most settings discussed in this post are configured in these Azure Data Factory activities.
Connecting To Azure Function
First, let’s figure out which Azure Data Factory Activity to use to invoke our Azure Function. There are many activities available, but we are interested in the following two:
- Azure Function Activity - purpose-built for calling Azure Functions, uses Azure Function linked service which also has functionality to allow retrieving Function Key from Key Vault. Interestingly, this linked service makes Function Key field required, even if your function doesn’t use authorization.
- Web Activity - generic functionality to call an HTTP endpoint, supports authentication mechanisms that aren’t available in Azure Function activity, for example, user-assigned managed identity, client certificate, etc. It also gives full control of the URL which might be necessary if your function has customized prefix route.
Azure Function activity is a reasonable default, which satisfies most needs. And if we have a special use case, then we can always switch to Web Activity. In the following sections, we will be using Azure Function Activity in the examples, and mention Web Activity wherever applicable.
NOTE: Passing parameters is very similar when using Azure Function activity and Web activity, so you should be good following the examples even if you are using Web activity.
1. Body Parameters
According to Wikipedia, HTTP message body is the data bytes transmitted in a message immediately following the headers. We can think of it as a request payload where we can send any data we want.
To pass parameters to Azure Function from Data Factory as part of the request body, you should know:
-
Request body is specified in the “Body” field of the “Settings” tab in Azure Function or Web activity. Note that the “Body” field will only be available for methods that support body, i.e.
POST
&PUT
. See the screenshot below for an example.
”Body” field:{"name": "Alex"}
-
POST
,PUT
, andPATCH
requests can contain request body. In theory, other requests likeGET
orDELETE
can have body as well, but it’s likely that those bodies will be ignored by the receiver since it’s against the general convention, so you shouldn’t use body with such methods. -
Content of the request body can be anything - it depends on what your Azure Function expects to receive. Often, the content is in JSON format, but XML, plain text, and form data formats are also common. It is a good practice to specify
Content-Type
header describing the type of the content in the request body, for example,application/json
in case of JSON. Additionally, we can use dynamic content to make our pipeline more flexible.
NOTE: Interestingly, as of March 2023, even though Azure Data Factory doesn’t support specifying the body for a DELETE
request (the body field is hidden), if you input the body for POST
or PUT
request, and then select back DELETE
method and send the request, ADF will actually send the DELETE
with the request body you specified. However, you shouldn’t rely on this behavior because it can change at any moment.
Here is the JSON code representation of the ADF pipeline. You can import this configuration into your Data Factory pipeline by clicking the “{ }” icon on the top right and pasting content there. Before saving, change “name” field in the JSON to match your pipeline name.
Pipeline JSON Code - Click to expand
Passing Azure Function parameters in request body
2. Query Parameters
Another common way to pass parameters as part of HTTP request is to use query string. It is a part of URL and specified after ?
, and query parameters are separated by &
, in the following example, we pass name
and age
parameters:
https://example.com/some/path?name=Alex&age=28
.
-
Query parameters are specified in the “Function name” field of the “Settings” tab in Azure Function activity (or URL field in Web activity) together with the function name. See the screenshot below. It is somewhat unintuitive to specify query parameters as part of a function name - it is likely that Data Factory uses the value from “Function name” field to create URL.
”Function name” field:Function1?name=Alex
-
In contrast to request body, query parameters can be specified for all types of HTTP requests. In the following example, we use
GET
request. Also, note that we can leverage dynamic content when specifying query parameters. -
If your function uses Function Key for authorization, the key must be somehow passed during an invocation. For Azure Function activity, it is usually done in the linked service configuration. However, another option is to pass the function key as
code
query parameter. I plan to write a separate post about authentication and authorization when calling Azure Function from Data Factory, so stay tuned, it will appear in the Related Posts section.
”Function name” field:Function1?name=Alex&code=7PIEewONq1GZS..
NOTE: URL maximum length. Query parameters contribute to the overall length of the URL. And even though HTTP specification doesn’t limit the length of URL, different browser and server implementations enforce their own limits. In general, it is safe to have URLs under 2048 characters, however, you should do your research if you plan to have longer query strings or URLs.
Here is the JSON code representation of the ADF pipeline. You can import this configuration into your Data Factory pipeline by clicking the “{ }” icon on the top right and pasting content there. Before saving, change “name” field in the JSON to match your pipeline name.
Pipeline JSON Code - Click to expand
Passing Azure Function parameters in query string
3. Path Parameters
Path is a part of URL which identifies a resource within the host. Or, in simple words, it’s a part of URL that goes right after the host and before the optional query (?age=28
). Path can consist of multiple parts separated by /
, in the following example of Azure Function URL, /api/users/alex
is path:
https://func-contoso.azurewebsites.net/api/users/alex?age=28
NOTE: In Azure Functions terminology, api
is route prefix, and users/alex
is route. Route prefix and route together form request path. Route prefix can be customized in host.json
for the entire Function App. Route can be customized for a particular function in function.json
(Python) or using attributes (C#).
Azure Functions allow customizing the request path and extracting parts of the route as parameters that can be accessed in the function body. Let’s assume that we defined our route to be users/{name}
, where name
is a route parameter.
-
Path (route) parameters are specified in the “Function name” field of the “Settings” tab in Azure Function activity (or URL field in Web activity) instead of the function name. See the screenshot below. “Function name” field naming is misleading, because the value of this field is rather route + query parameters, and in the default case this value is the same as the function name.
”Function name” field:users/alex
-
Azure Function activity requires route prefix to be
api
- this is the default setting when creating a Function App. If your function has a customizedextensions.http.routePrefix
property in thehost.json
file, then we need to use Web activity, there we can specify the full URL of the function including the custom route prefix.
Here is the JSON code representation of the ADF pipeline. You can import this configuration into your Data Factory pipeline by clicking the “{ }” icon on the top right and pasting content there. Before saving, change “name” field in the JSON to match your pipeline name.
Pipeline JSON Code - Click to expand
Passing Azure Function parameters in request path
4. Header Parameters
HTTP headers are a part of HTTP request or response where we can provide additional context or metadata, they can be standard headers like Content-Type
as well as custom ones. Note that any HTTP request can have headers.
-
Header parameters are specified in the “Headers” field of the “Settings” tab in Azure Function activity or Web activity. See the screenshot below for an example. As with other types of parameters, we can use dynamic content to make our pipeline more flexible.
-
If you use Function Keys to secure your Azure Function, then you should pass the key during the invocation. The options to specify Function Key are: (a) in the linked service configuration, (b) as
code
query parameter, or (c) asx-functions-key
header.
x-functions-key: 7PIEewONq1GZS..
NOTE: Header names are case-insensitive, this means that Content-Type
and content-type
header names are equivalent. However, header values can be case-sensitive depending on the use case.
Here is the JSON code representation of the ADF pipeline. You can import this configuration into your Data Factory pipeline by clicking the “{ }” icon on the top right and pasting content there. Before saving, change “name” field in the JSON to match your pipeline name.
Pipeline JSON Code - Click to expand
Passing Azure Function parameters in request headers
HTTP Methods: GET, POST, PUT, DELETE, & Others
Azure Function can be triggered in various ways, one of them is through an HTTP request, this can be achieved by using Azure Functions HTTP trigger. After configuring the HTTP trigger, we get an HTTP endpoint in the following format (path can be customized, see path parameters):
https://<function_app_name>.azurewebsites.net/api/<function_name
For example, https://func-contoso.azurewebsites.net/api/Function1
Azure Function can accept any HTTP method, e.g. GET
, POST
, DELETE
, etc. To make your Azure Function work with a particular HTTP method, we need to do the following two things:
- Specify that you want to handle a particular HTTP method in
function.json
(e.g. Python or JavaScript) or in the corresponding attribute (e.g. C# or Java). - Write the function code to handle the request accordingly. Actually, the function can be accept all HTTP methods and have the same code that handles it. However, it’s probably not very useful.
When calling an Azure Function from Azure Data Factory, ultimately, we should use the HTTP method that our Azure Function supports and expects.
Summary
In this post, we explored how to pass parameters into Azure Function from Azure Data Factory in request body, query string, request path, and HTTP headers.
It’s important to remember that the way you pass parameters/arguments to your Azure Function depends on the function’s implementation, so you should take time to understand what parameters it expects.
With regards to Azure Data Factory, it provides all necessary means to call an Azure Function and pass parameters in the ways we discussed above. In this post, we focused on Azure Function and Web activities.
There are additional considerations that we need to make when using Azure Functions from Data Factory. Some topics such as authentication and function output will be covered in next posts, so stay tuned.
Related Posts
- Dates & Timestamps In Azure Data Factory: Parsing, Formatting, Converting - Pipeline & Data Flow
- 3 Ways To Check If Data Exists Using ADF Data Flow - Azure Data Factory