Azure function basic code

#r “Newtonsoft.Json”

using System.Net;
using Newtonsoft.Json;
using System.Text;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
log.Info(“C# HTTP trigger function processed a request.”);

var data = await req.Content.ReadAsStringAsync();

var json = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(data);
json.Remove(“etag”);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json.ToString(), Encoding.UTF8, “application/json”)
};

}
catch (Exception ex)
{
HttpResponseMessage res = req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
return res;
}
}

// check data and return

log.Info(“C# HTTP trigger function processed a request.”);

string data = await req.Content.ReadAsStringAsync();

XmlDocument doc = new XmlDocument();
doc.LoadXml(data);

var node = doc.DocumentElement.SelectSingleNode(“//Root/@SubAttribute”);

var returnobj = new { MessageType = node.Value };
var jsonToReturn = JsonConvert.SerializeObject(returnobj);

return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonToReturn, Encoding.UTF8, “application/json”)
};

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s