how to Retrieve document from CosmosDB in HTTP Trigger Azure Function developed with C# Script on Azure Portal?

sv 11 Reputation points
2022-06-10T18:33:27.237+00:00

I have function.json:

{  
    "bindings": [  
        {  
            "authLevel": "function",  
            "name": "req",  
            "type": "httpTrigger",  
            "direction": "in",  
            "methods": [  
                "get",  
                "post"  
            ]  
        },  
        {  
            "name": "$return",  
            "type": "http",  
            "direction": "out"  
        },  
        {  
            "name": "inputDocument",  
            "databaseName": "wiki",  
            "collectionName": "wiki",  
            "connectionStringSetting": "oio_DOCUMENTDB",  
            "id": "{Query.id}",  
            "partitionKey": "/type",  
            "sqlQuery": "SELECT * FROM c WHERE c.id = {Query.id}",  
            "direction": "in",  
            "type": "cosmosDB"  
        }  
    ]  
}  

and run.csx:

#r "Newtonsoft.Json"  
  
using System.Net;  
using Microsoft.AspNetCore.Mvc;  
using Microsoft.Extensions.Primitives;  
using Newtonsoft.Json;  
  
public static async Task<IActionResult> Run(HttpRequest req, inputDocument, ILogger log)  
{  
    log.LogInformation("C# HTTP trigger function processed a request.");  
  
    string name = req.Query["id"];   
  
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();  
    dynamic data = JsonConvert.DeserializeObject(requestBody);  
    name = name ?? data?.name;  
  
    string responseMessage = string.IsNullOrEmpty(name)  
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."  
                : $"Hello, {inputDocument}. This HTTP triggered function executed successfully.";  
  
    return new OkObjectResult(responseMessage);  
}  

how to pass inputDocument into Function correctly?

all examples from https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb?tabs=csharp-script#http-trigger-look-up-id-from-route-data-c-script
not working.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jaliya Udagedara 2,856 Reputation points MVP Volunteer Moderator
    2022-06-10T20:25:00.957+00:00

    Seems the documentation you are referring is for Azure Functions 1.x.

    Can you follow this please.
    Azure Cosmos DB input binding for Azure Functions 2.x and higher

    Note:

    • If you are getting by Id, the query is not needed

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.