I was wondering how to use Azure Function to insert data in CosmosDB when this function receives an HTTP request
Surprise!! It´s easy
Step 1. Create your DataBase and Collection inside CosmosDB in Azure.
Step 2. Create Function App
Step 3. Create a new function. In this case use template "HttpTrigger - C#"
Step 4. Select "Integrate" option
Be sure to change option in "Allowed HTTP methods" and select only "POST" HTTP method.
Step 5. Create output binding
Step 6. Configuring output binding
a: Name of the entity variable to insert in CosmosDB.
b: Name of the collection where the entity will be inserted (see image in Step 1).
c: Partition Key in case you need to define it.
d: DataBase name in Cosmos DB (see image in Step 1).
e: ConnectionString.
Step 7. "Short" programming....
a: Entity variable referenced in Step 6. a
b: Entity type (custom class).
outputDocument= req.Content.ReadAsAsync<DTODocument>().Result;
The function of this line is to read the body content in the request and deserialized it to the variable of DTODocument type.
Step 8. Testing
a: Get Azure Function endpoint to invoque it from any programming language.
b: Request body example in JSON format (DTODocument)
c: Also you can test the function inside Azure Function App context.
See you late !!!
Comments