Trying to build a couple simple http Azure functions. One would take a POST from a web app, containing a JSON body, and save that as a document in CosmosDB (DocumentDB).
To store data in Cosmos DB from HttpTrigger Azure functions app, you can refer to the following sample code that works fine on my side.
using System.Net;
public static HttpResponseMessage Run(HttpRequestMessage req, out object taskDocument, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
MyData md=req.Content.ReadAsAsync<MyData>().Result;
taskDocument = new {
name = md.name,
task = md.task,
duedate = md.duedate
};
if (name != "") {
return req.CreateResponse(HttpStatusCode.OK);
}
else {
return req.CreateResponse(HttpStatusCode.BadRequest);
}
}
public class MyData{
public string name { get; set;}
public string task { get; set;}
public string duedate { get; set;}
}


The other sends a GET request with a parameter, which reads that document from the database and returns it as JSON.
To retrieve data from Cosmos DB and return it as JSON via Azure functions app, please refer to the following sample.
function.json
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "documents/{name}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "inputDocument",
"databaseName": "xxxdocumentdbtest",
"collectionName": "testcoll",
"sqlQuery": "SELECT * FROM c where c.name = {name}",
"connection": "xxxx_DOCUMENTDB",
"direction": "in"
}
],
"disabled": false
}
run.csx
#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
public static HttpResponseMessage Run(HttpRequestMessage req, IEnumerable<MyData> inputDocument, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
MyData md = inputDocument.FirstOrDefault();
log.Info(md.task);
var val = JsonConvert.SerializeObject(md);
return req.CreateResponse(HttpStatusCode.OK, val);
}
public class MyData{
public string name { get; set;}
public string task { get; set;}
public string duedate { get; set;}
}
