-2

I need some help in writing an Azure function with CosmosDB trigger, which will capture some values from cosmosdb like below and create a POST call and trigger an API. Is it possible in Azure function? Cosmosdb: enter image description here

The POST API which needs to be passed through Azure function is like this.

enter image description here

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Please edit your question to include properly-formatted text, not images of text. This [meta post](https://meta.stackoverflow.com/a/285557/272109) lists many reasons why this is important. Also, please show what you've tried so far (code, errors, etc). It's unclear where, exactly, you're stuck. – David Makogon Feb 10 '22 at 13:55
  • You want your azure function to make a HTTP POST API call - it is very much possible. See this SO [question](https://stackoverflow.com/questions/42482223/azure-functions-call-http-post-inside-function) – Anand Sowmithiran Feb 10 '22 at 14:06

1 Answers1

0

Here is the example code to for passing POST in the Azure function

[FunctionName("TestPost")]
public static HttpResponseMessage POST([HttpTrigger(AuthorizationLevel.Function, "put", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
    try
    {
        //create redis connection and database
        var RedisConnection = RedisConnectionFactory.GetConnection();
        var serializer = new NewtonsoftSerializer();
        var cacheClient = new StackExchangeRedisCacheClient(RedisConnection, serializer);

        //read json object from request body
        var content = req.Content;
        string JsonContent = content.ReadAsStringAsync().Result;

        var expirytime = DateTime.Now.AddHours(Convert.ToInt16(ConfigurationSettings.AppSettings["ExpiresAt"]));

        SessionModel ObjModel = JsonConvert.DeserializeObject<SessionModel>(JsonContent);
        bool added = cacheClient.Add("RedisKey", ObjModel, expirytime); //store to cache 

        return req.CreateResponse(HttpStatusCode.OK, "RedisKey");
    }
    catch (Exception ex)
    {
        return req.CreateErrorResponse(HttpStatusCode.InternalServerError, "an error has occured");
    }
}

Here are the few examples for Post in Azure functions

Azure function POST

Post Async Function.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15