I have an Azure Function in C# which I have almost built. It is setup to receive data from SendGrid, verify the data and then write the data to Cosmos. Here is the code:
namespace SendGrid.CosmosLogging
{
public static class SendGrid_Logging
{
[FunctionName("SendGrid_Logging")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req,
[CosmosDB(
databaseName: "portal-dev",
collectionName: "sendgrid",
ConnectionStringSetting = "CosmosDbConnectionString")]
IAsyncCollector<dynamic> documentsOut,
ILogger log)
{
try
{
log.LogInformation("SendGrid C# HTTP trigger function started processing a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var headers = req.Headers;
var sgbody = req.Body;
//Check validity
CheckValidity(log, documentsOut, sgbody); //req.Body - has to be Stream object
}
catch (Exception e)
{
log.LogInformation("Error validating SendGrid event, error is: "+ e.ToString());
throw;
}
}
private static async void CheckValidity(ILogger log, IAsyncCollector<dynamic> documentsOut, Stream sgBody)
{
// Add a JSON document to the output container.
await documentsOut.AddAsync( new
{
//Write the sendgrid body only to the Cosmos container
sgBody
//Body = "This is something"
//name = name
});
responseMessage =
"The SendGrid HTTP triggered function executed successfully processing Id: ";//${doc_id}
log.LogInformation(responseMessage);
}
}
}
The problem is the line writing the sgBody during the CheckValidity method. This never comes through to the Cosmos container. However if it is replaced with something like:
// Add a JSON document to the output container.
await documentsOut.AddAsync( new
{
//Write the sendgrid body only to the Cosmos container
//sgBody
Body = "This is something"
//name = name
});
Then I get an output that looks like this in Cosmos:
{
"Body": "This is something",
"id": "e65c2efe-79d2-4997-b1b2-33833dbf14ce",
"_rid": "2WwUAKT2MsvdKgAAAAAAAA==",
"_self": "dbs/2WwUAA==/colls/2WwUAKT2Mss=/docs/2WwUAKT2MsvdKgAAAAAAAA==/",
"_etag": "\"12015ff1-0000-1a00-0000-61679d8c0000\"",
"_attachments": "attachments/",
"_ts": 1634180492
}
So my question is, how do I get the entire contents of the sgBody variable to write? There is something I am clearly missing here (I aren't a C# developer, but am trying my best!).
EDIT:
The format I am wanting to get as output to the Cosmos container is a properly formatted JSON body, something like:
{
"body":{
"timestamp":1631141801,
"sg_template_name":"Receiver - NonProd",
"ip":"149.72.246.163",
"tls":1,
"smtp-id":"<2MhvBnobRN-KQNdFF9cO4w@geopod-ismtpd-5-0>",
"email":"somewhere@something.com",
"response":"250 2.6.0 <2MhvBnobRN-KQNdFF9cO4w@geopod-ismtpd-5-0> [InternalId=300647724419, Hostname=SY4P282MB3809.AUSP282.PROD.OUTLOOK.COM] 289934 bytes in 0.131, 2154.683 KB/sec Queued mail for delivery",
"sg_message_id":"2MhvBnobRN-KQNdFF9cO4w.filterdrecv-75ff7b5ffb-vm78l-1-61393FA4-43.0",
"event":"delivered",
"sg_event_id":"ZGVsaXZlcmVkLTAtMjI4MDI0MTAtMk1odkJub2JSTi1LUU5kRkY5Y080dy0w",
"sg_template_id":"d-04096fb423674bdf8870dfc92eec944f",
"category":[
"develop",
"Something"
]
},
"id":"0c4143fa-d5a2-43e8-864f-82f333ace3cd",
"_rid":"SL81APS-4Q21KQAAAAAAAA==",
"_self":"dbs/SL81AA==/colls/SL81APS-4Q0=/docs/SL81APS-4Q21KQAAAAAAAA==/",
"_etag":"\"7700726c-0000-1a00-0000-61393fb20000\"",
"_attachments":"attachments/",
"_ts":1631141810
}