Thank you @MughundhanRaveendran-MSFT .
I was able to get it to work by modifying some of the code and here is the final version which is working for me and this might not be the perfect way to achieve this but, serving my purpose. Hope this helps someone with similar need.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Blobs;
namespace Webhook
{
public static class ThirdPartyWebhook
{
[FunctionName("ThirdPartyWebhook")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("ThirdParty Payload has been captured...");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string env_connstring = Environment.GetEnvironmentVariable("connectionstring");
string env_container = Environment.GetEnvironmentVariable("container");
string messageContent = $"{data}";
WriteToBlob(env_connstring, env_container, messageContent);
return new OkObjectResult(messageContent);
}
public static void WriteToBlob(string env_connstring, string env_container, string content)
{
//string filename = $"{DateTime.Now.Ticks}.json";
string fileName = "Events_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".json";
string filePath = $"ThirdParty/WebhookPayload/{fileName}";
BlobContainerClient containerClient = new(env_connstring, env_container);
BlobClient blobClient = containerClient.GetBlobClient(filePath);
blobClient.Upload(BinaryData.FromString(content), overwrite: true);
}
}
}
Open for optimization suggestions.
Thank you!