1
votes

I have created an Azure Function that gets triggered with an HTTP POST request.
The request has a body like the following:

{
    "start": '2018-07-25T08:47:16.094Z',
    "end": '2018-07-25T08:47:24.686Z'
}

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    var inputDocument = context.bindings.inputDocument;
    context.log("INPUT DOCUMENT: " + JSON.stringify(inputDocument));
    context.res = {
            status: 200,
            body: "OK",
        };
};

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "cosmosDB",
      "name": "inputDocument",
      "databaseName": "Messages",
      "collectionName": "Collection1",
      "sqlQuery": "SELECT * FROM Collection1 c WHERE c.message.timestamp>={start} AND c.message.timestamp<={end}",
      "connectionStringSetting": "cosmosdbaccount_DOCUMENTDB",
      "direction": "in"
    }
  ]
}

I would like to query a CosmosDB instance with these two parameters like this:

"sqlQuery": "SELECT * FROM Collection1 c WHERE c.message.timestamp>={start} AND c.message.timestamp<={end}",

Doing it like I show here results in the CosmosDB's inputBinding beeing undefined.

2018-12-13T08:19:54.332 [Information] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=af8090a4-5fab-4fbd-b26f-a045d8900d9b)
2018-12-13T08:19:56.704 [Information] JavaScript HTTP trigger function processed a request.
2018-12-13T08:19:56.711 [Information] INPUT DOCUMENT: []
2018-12-13T08:19:56.755 [Information] Executed 'Functions.Function1' (Succeeded, Id=af8090a4-5fab-4fbd-b26f-a045d8900d9b)

If I run the same exact query replacing start and end with the correct ISO timestamps in the CosmosDB Data Explorer it returns all four documents.

Am I missing something? I really didn't find anything about this topic online so I hope someone has already stumbled upon it.

Thanks a lot in advance!

1

1 Answers

3
votes

The empty result seems expected for now. In CosmosDB bingding, sqlQuery has difficulty handling nested token(start and end are properties of request body), related issue here.

Here are three workarounds to try.

  1. Simply put start and end in query string, like https://functionUrl?start=<timeA>&end=<timeB>.

  2. In function.json, set route parameter start and end, i.e. add "route": "{start}/{end}", in httpTrigger binding. Then put time in url to visit.

  3. Get request body and query CosmosDB on our own.