8

I am trying to run an http trigger Azure function with Cosmos DB input binding. I would like the url of the http trigger to include several parameters on the query string that become bound to the SQL query of the input Cosmos DB binding. I am trying the following bindings in function.json, but it does not work (the function does not even get triggered):

{
  "direction": "in",
  "type": "httpTrigger",
  "authLevel": "anonymous",
  "name": "req",
  "methods": [ "get" ],
  "route": "users/{age=age?}/{gender=gender?}"
},
{
  "direction": "in",
  "type": "documentDB",
  "name": "users",
  "databaseName": "Database",
  "collectionName": "Users",
  "sqlQuery": "SELECT * FROM x where x.age = {age} and x.gender = {gender}",
  "connection": "COSMOSDB_CONNECTION_STRING"
},

According to this answer the route constraint users/{age=age?}/{gender=gender?} is valid for Web API, and according to the documentation you can use any Web API Route Constraint with your parameters. Ultimately I would to like make a GET request to the Azure function that looks like api/users?age=30&gender=male. How should this be done then?

Carlos Rodriguez
  • 523
  • 1
  • 5
  • 13

3 Answers3

6

I don't think you can configure a Cosmos DB binding to values defined in query parameters e.g. ?age=30. At least I haven't seen any examples like that in the functions documentation.

But you can bind them to the route parameters to achieve the same outcome, which you have pretty much done already.

Keeping that route of users/{age}/{gender}, your Cosmos SqlQuery will then pick up those route parameters when invoking a GET on http://yourfunctionhost/yourfunction/users/30/male

Garth Mason
  • 7,611
  • 3
  • 30
  • 39
  • I was afraid that would be the case, since I also could not find any examples binding to parameters in the query string. Thank you for confirming. – Carlos Rodriguez Aug 21 '17 at 19:05
  • that is not true GET parameter will be bound and they can be used in the sqlQuery. Maybe this has changed in the past – cinatic May 24 '18 at 07:10
1

GET and POST parameter will be bound, so they can be used inside the sqlQuery without any additional configuration. Just give it a try, this has definitely changed in the past

cinatic
  • 861
  • 12
  • 19
0

You can bind the Query-Values to CosmosDB Input Binding:
Azure Cosmos DB input binding for Azure Functions 2.x and higher

[FunctionName("DocByIdFromQueryString")]
public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
        HttpRequest req,
    [CosmosDB(
        databaseName: "ToDoItems",
        collectionName: "Items",
        ConnectionStringSetting = "CosmosDBConnection",
        Id = "{Query.id}",
        PartitionKey = "{Query.partitionKey}")] ToDoItem toDoItem,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    if (toDoItem == null)
    {
        log.LogInformation($"ToDo item not found");
    }
    else
    {
        log.LogInformation($"Found ToDo item, Description={toDoItem.Description}");
    }
    return new OkResult();
}
lukaszberwid
  • 1,097
  • 7
  • 19
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
  • One doubt! if the `Route` is set to `null`, then what would be the final API endpoint? – rehman_00001 Jul 07 '22 at 11:47
  • 1
    It's the FunctionName 'DocByIdFromQueryString'. On local environment: http://localhost:7071/api/DocByIdFromQueryString MS Docs: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-your-first-function-visual-studio?tabs=in-process#rename-the-function – Markus Meyer Jul 07 '22 at 11:55