Azure http function and DocumentDB -
trying build couple simple http azure functions. 1 take post web app, containing json body, , save document in cosmosdb (documentdb). other sends request parameter, reads document database , returns json.
i have documentdb collection setup , ready.
every example find comes close has slight difference, outputting queue, example not quite need.
trying build couple simple http azure functions. 1 take post web app, containing json body, , save document in cosmosdb (documentdb).
to store data in cosmos db httptrigger azure functions app, can refer following sample code works fine on side.
using system.net; public static httpresponsemessage run(httprequestmessage req, out object taskdocument, tracewriter log) { log.info("c# http trigger function processed 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 request parameter, reads document database , returns json.
to retrieve data cosmos db , return json via azure functions app, please refer 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 * c 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 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;} }
Comments
Post a Comment